移除wp_NAV_MENU中不需要的元素

时间:2019-09-27 作者:bigant841

我正在Wordpress中构建一个自定义的超大菜单。我遇到了一个问题,wp\\U nav\\U菜单中显示的不需要的元素正在扭曲我的菜单。

<?php
    wp_nav_menu( array(
       \'theme_location\' => \'main-menu\',
       \'menu_id\'            => \'primary-menu\',
       \'items_wrap\'     => \'<ul class="nav-menu row">%3$s</ul>\',
       \'walker\'         => new custom_menu,
       \'container\'         => \'false\',
       \'menu_class\'        => \'false\',
    ) );
 ?>
我正在呼叫自定义walker,以便显示特定的div和order列表,如下所示:

class custom_menu extends Walker_Nav_Menu {
function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\\t", $depth);
    $output .= "\\n$indent<ul><div class=\\"container\\">\\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\\t", $depth);
    $output .= "$indent</ul></div>\\n";
}
}
 add_filter(\'nav_menu_css_class\', \'my_css_attributes_filter\', 100, 1);
 add_filter(\'nav_menu_item_id\', \'my_css_attributes_filter\', 100, 1);
 add_filter(\'page_css_class\', \'my_css_attributes_filter\', 100, 1);
 function my_css_attributes_filter($var) {
   return is_array($var) ? array_intersect($var, array(\'sub-menu-columns\')) : \'\';
}
下面是它显示的HTML。我还将需要移除的元素推向市场。

 <div class="menu">
    <ul class="nav-menu row">
        <li><a href="http://dccollegenew.wpengine.com/admission-aspx/">Admissions and Aid</a>
            <ul>
                <li style="list-style: none; display: inline"><!-- REMOVE -->
                    <div class="container">
                        <ul><!-- REMOVE -->
                            <li><h2>Admissions</h2>
                                <div class="menu-admissions-delivery-systems-container"><!-- REMOVE -->
                                    <ul class="menu" id="menu-admissions-delivery-systems"><!-- REMOVE CLASS AND ID-->
                                        <li><a href="http://dccollegenew.wpengine.com/freshmen-admissions/">Freshmen Admissions</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/transfer/">Transfer Admissions</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/online-courses/">Online Programs</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/adult-programs/">Adult Students</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/graduate-admissions/">Graduate Admissions</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/international-students/">International Students</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/veterans/">Veterans and Military Students</a></li>
                                    </ul>
                                </div><!-- REMOVE -->
                            </li>
                            <li>
                                <h2>Financial Aid</h2>
                                <div class="menu-admissions-financial-aid-container"><!-- REMOVE -->
                                    <ul class="menu" id="menu-admissions-financial-aid"><!-- REMOVE CLASS AND ID-->
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/">Financial Aid</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/scholarship-financial-aid-options/">Scholarship and Financial Aid Options</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/financial-aid-forms/">Financial Aid Forms</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/contact-financial-aid-counselor/">Contact a Financial Aid Counselor</a></li>
                                        <li><a href="http://dccollegenew.wpengine.com/admissions/financial-aid-2/net-price-calculator/">Net Price Calculator</a></li>
                                    </ul>
                                </div><!-- REMOVE -->
                            </li>
                        </ul><!-- REMOVE -->
                    </div>
                </li>
            </ul>
        </li>
    </ul>
</div>
这是菜单的显示方式

 <section class="bottom-header container-fluid">
 <div class="menu">
    <ul class="clearfix nav-menu">
        <li><a href="#">Admissions and Aid</a>
            <ul>
                <div class="container">
                    <li><h2>School</h2>
                        <ul>
                            <li><a href="#">Lidership</a></li>
                            <li><a href="#">History</a></li>
                            <li><a href="#">Locations</a></li>
                            <li><a href="#">Careers</a></li>
                        </ul>
                    </li>
                    <li><h2>School</h2>
                        <ul>
                            <li><a href="#">Undergraduate</a></li>
                            <li><a href="#">Masters</a></li>
                            <li><a href="#">International</a></li>
                            <li><a href="#">Online</a></li>
                        </ul>
                    </li>
                    <li><h2>School</h2>
                        <ul>
                            <li><a href="#">Undergraduate research</a></li>
                            <li><a href="#">Masters research</a></li>
                            <li><a href="#">Funding</a></li>
                        </ul>
                    </li>
                    <li><h2>School</h2>
                        <ul>
                            <li><a href="#">Undergraduate research</a></li>
                            <li><a href="#">Masters research</a></li>
                            <li><a href="#">Funding</a></li>
                        </ul>
                    </li>
                </div>
            </ul>
        </li>
        <li><a href="#">Academics</a></li>
        <li><a href="#">Student Life</a></li>               
        <li><a href="#">Athletics</a></li>  
        <li><a href="#">Alumni</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Support The College</a></li>
    </ul>
 </div>
 </section>
无论我做什么,我似乎都无法删除我标记为“删除”的项目。任何帮助都将不胜感激。

1 个回复
SO网友:bigant841

我通过重新配置助行器来实现它

 class custom_menu extends Walker_Nav_Menu {
   function start_el(&$output, $item, $depth, $args) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "\\t", $depth ) : \'\';

    $class_names = $value = \'\';

    $classes = empty( $item->classes ) ? array() : (array) $item->classes;

    $class_names = join( \' \', apply_filters( \'nav_menu_css_class\',  array_filter( $classes ), $item ) );
    $class_names = \' class="\' . esc_attr( $class_names ) . \'"\';

    $output .= $indent . \'<li>\';

    $attributes = ! empty( $item->attr_title ) ? \' title="\' . esc_attr( $item->attr_title ) .\'"\' : \'\';
    $attributes .= ! empty( $item->target ) ? \' target="\' . esc_attr( $item->target ) .\'"\' : \'\';
    $attributes .= ! empty( $item->xfn ) ? \' rel="\' . esc_attr( $item->xfn ) .\'"\' : \'\';
    $attributes .= ! empty( $item->url ) ? \' href="\' . esc_attr( $item->url ) .\'"\' : \'\';

    $item_output = $args->before;
    $item_output .= \'<a\'. $attributes .\'>\';
    $item_output .= $args->link_before . apply_filters( \'the_title\', $item->title, $item->ID ) . $args->link_after;
    $item_output .= \'</a>\';
    $item_output .= $args->after;

    $output .= apply_filters( \'walker_nav_menu_start_el\', $item_output, $item, $depth, $args );
}
function start_lvl(&$output, $depth) {
    $indent = str_repeat("\\t", $depth);
    $output .= "\\n$indent<ul><div class=\\"container\\">\\n";
  }
 }
  add_filter(\'nav_menu_css_class\', \'my_css_attributes_filter\', 100, 1);
  add_filter(\'nav_menu_item_id\', \'my_css_attributes_filter\', 100, 1);
  add_filter(\'page_css_class\', \'my_css_attributes_filter\', 100, 1);
    function my_css_attributes_filter($var) {
    return is_array($var) ? array_intersect($var, array(\'sub-menu-columns\'))    : \'\';
 }

相关推荐

带有ACF字段的子菜单的Walker类

首先,我想知道这是否是最好的方法。我有一个基本导航,一个项目有孩子。子级需要包装在容器div中,每个子级需要从其关联页面中提取一些ACF值(图像、ACF repeater值)。孩子们基本上是内容+背景图像的平铺。学步班是实现这一目标的最佳方式吗?我唯一能做的就是影响所有nav项目,我不确定如何使子项目受到影响并引入ACF值。谢谢