user3643520
user3643520

Reputation: 105

menu walker - add span around page title in the A link of top level items in a WordPress nav menu

Just wondering if someone can please help me with the code to add to an existing menu walker in a WordPress 4.7.4 nav menu called "main" to add a span around the page title in the A link to all top level items.

I would like the (simplified) html to look like so:

<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>

The walker in my functions.php file currently has code in it to add a wrapper div around sub menu items for a drop down menu. This works great and is shown below:

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 and link_after attributes in the menu call don't seem appropriate because they add spans to all menu items including sub pages.

I found some functions.php code for adding a span around top level items but this adds spans to the top level items of all WP menus, not just the menu called "main". This code is show below:

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;
});

Can the above code work inside a menu walker for the one menu called main? If so, how do I implement that? Or is new code required for the menu walker? If so, any ideas?

Thanks in advance for any help.

Upvotes: 1

Views: 2112

Answers (1)

Vipin
Vipin

Reputation: 447

Hey i have implemented a code as per my Project the menu html is same.

Hope this will help you :-)

PLease check below code for 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>

Check my Header menu code in Header.php file

<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>

Check below code for walker menu put in function.php file

<?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 );
    }
}

?>

Upvotes: 2

Related Questions