menu walker - 在WordPress导航菜单的顶级项目的A链接中添加页面标题周围的跨度

时间:2017-05-12 10:33:28

标签: php html wordpress

只是想知道是否有人可以帮助我将代码添加到WordPress 4.7.4导航菜单中的现有菜单助手中,该菜单名为" main"在所有顶级项目的A链接中添加页面标题周围的范围。

我希望(简化的)html看起来像这样:

<ul>
    <li><a href="#" title="Page 1"><span>Page 1</span></a></li>
    <li><a href="#" title="Page 2"><span>Page 2</span></a><div class="drop">
        <ul>
            <li><a href="#" title="Sub Page 2.1">Sub Page 2.1</a></li>
            <li><a href="#" title="Sub Page 2.2">Sub Page 2.2</a></li>
            <li><a href="#" title="Sub Page 2.3">Sub Page 2.3</a></li>
            <li><a href="#" title="Sub Page 2.4">Sub Page 2.4</a></li>
        </ul>
    </div></li>
    <li><a href="#" title="Page 3"><span>Page 3</span></a></li>
    <li><a href="#" title="Page 4"><span>Page 4</span></a><div class="drop">
        <ul>
            <li><a href="#" title="Sub Page 4.1">Sub Page 4.1</a></li>
            <li><a href="#" title="Sub Page 4.2">Sub Page 4.2</a></li>
        </ul>
    </div></li>
    <li><a href="#" title="Page 5"><span>Page 5</span></a></li>
    <li><a href="#" title="Page 6"><span>Page 6</span></a></li>
    <li><a href="#" title="Page 7"><span>Page 7</span></a></li>
</ul>

我的functions.php文件中的walker当前包含代码,用于在下拉菜单的子菜单项周围添加包装div。这很有效,如下所示:

class main_menu_walker extends Walker_Nav_Menu
{
    function start_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n$indent<div class=\"drop\"><ul>\n";
    }
    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
}

菜单调用中的Link_before和link_after属性似乎不合适,因为它们会向所有菜单项(包括子页面)添加跨度。

我发现了一些用于在顶级项目周围添加跨度的functions.php代码,但这会为所有WP菜单的顶级项目添加跨度,而不仅仅是名为&#34; main&#34;的菜单。此代码如下所示:

add_filter( 'wp_nav_menu_objects', function( $items ) {
      foreach ( $items as $item ) {
        if (!$item->menu_item_parent) {
           $item->title = '<span>' . $item->title . '</span>';
        }
    }
    return $items;
});

以上代码是否可以在名为main的一个菜单的菜单步进器内工作?如果是这样,我该如何实现?或者是菜单步行者需要新代码?如果是这样,有什么想法吗?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

嘿,我按照我的项目实现了一个代码,菜单html是相同的。

希望这会对你有所帮助: - )

检查下面的HTML代码

<nav class="wsmenu slideLeft clearfix">
<ul class="mobile-sub wsmenu-list">
<li><a href="index.html" class="active"> Menu 1</a></li>
<li><a href="services.html">Menu 2 <span class="arrow"></span></a>
<ul class="wsmenu-submenu">
<li><a href="#"> sub Menu 2</a>
<ul class="wsmenu-submenu-sub">
<li><a href="#">sub sub Menu 2</a></li>
<li><a href="#">sub sub Menu 2</a></li>
<li><a href="#">sub sub Menu 2</a></li>
</ul>
</li>
<li><a href="#">sub Menu 2</a>
<ul class="wsmenu-submenu-sub">
<li><a href="#">sub sub Menu 2</a></li>
<li><a href="#">sub sub Menu 2</a></li>
<li><a href="#">sub sub Menu 2</a></li>
</ul>
</li>
<li><a href="#">Onsite Gear Measurement</a></li>
</ul>
</li>
<li><a href="">Menu 3</a></li>
<li><a href="">Menu 4</a></li>
</ul>
</nav>

检查Header.php文件中的Header菜单代码

<nav class="wsmenu slideLeft clearfix">
<!--<ul class="mobile-sub wsmenu-list">-->
<?php
wp_nav_menu( array( 'theme_location' => 'primary',
'menu_class' => 'mobile-sub wsmenu-list', 
'menu_id' => '', 
'container' => false, 
'walker' => new WPDocs_Walker_Nav_Menu()) ); 
?>   
</nav>

检查以下代码中的walker菜单放在function.php文件中

<?php 
class WPDocs_Walker_Nav_Menu extends Walker_Nav_Menu {

    /**
     * Starts the list before the elements are added.
     *
     * Adds classes to the unordered list sub-menus.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     */
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        // Depth-dependent classes.
        $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $classes = array(
            'wsmenu-submenu',
            ( $display_depth % 2  ? 'menu-odd' : 'menu-even' ),
            ( $display_depth >=2 ? 'wsmenu-submenu-sub' : '' ),
            'menu-depth-' . $display_depth
        );
        $class_names = implode( ' ', $classes );

        // Build HTML for output.
        $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
    }

    /**
     * Start the element output.
     *
     * Adds main/sub-classes to the list items and links.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param object $item   Menu item data object.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     * @param int    $id     Current item ID.
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent

        // Depth-dependent classes.
        $depth_classes = array(
            ( $depth == 0 ? 'main-menu-item' : 'sub-menu' ),
            ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
            ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
            'menu-item-depth-' . $depth
        );
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );

        // Passed classes.
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );

        // Build HTML.
        $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';

        // Link attributes.
        $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        ) .'"' : '';
        $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';

        // Build HTML output and pass through the proper filter.
        $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s<span></span></a>%6$s',
            $args->before,
            $attributes,
            $args->link_before,
            apply_filters( 'the_title', $item->title, $item->ID ),
            $args->link_after,
            $args->after
        );
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

?>