2

I have a custom nav walker I'm trying to use to output only the first level of a menu:

<?php wp_nav_menu( array(
    'theme_location' => 'secondary-menu',
    'depth' => 1,
    'walker' => new SecondaryNavWalker
) ); ?>

However, all levels of the navigation are outputted regardless. I've tried doing a check against the arg in start_el, but the depth argument passed in is always zero even when the page has a parent. What am I missing?

Here's the full custom nav walker:

class SecondaryNavWalker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth, $args)
    {
        //var_dump($item);
        //var_dump('Args');
        //var_dump($args);

        //var_dump('depth comp');
        //var_dump($depth);
        //var_dump($args['depth']);

        if (get_post_meta( $item->ID, 'saltIsInMainNav', true ) || $depth > $args['depth']) {
            return;
        }

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

        $class_names = join(
            ' '
            ,   apply_filters(
                'nav_menu_css_class'
                ,   array_filter( $classes ), $item
            )
        );

        ! empty ( $class_names )
        and $class_names = ' class="'. esc_attr( $class_names ) . '"';

        $output .= "<li id='menu-item-$item->ID' $class_names>";

        $attributes  = '';

        ! empty( $item->post_title )
        and $attributes .= ' title="'  . esc_attr( $item->post_title ) .'"';
        ! empty( $item->guid )
        and $attributes .= ' href="' . esc_url( site_url('/?p='.$item->ID) ) .'"';

        $title = apply_filters( 'the_title', $item->post_title, $item->ID );

        $item_output = $args->before
            . "<a $attributes>"
            . $args->link_before
            . "$title"
            . $args->link_after
            . '</a> '
            . $args->after;

        // Since $output is called by reference we don't need to return anything.
        $output .= apply_filters(
            'walker_nav_menu_start_el'
            ,   $item_output
            ,   $item
            ,   $depth
            ,   $args
        );
    }
}

When uncommented, those depth var_dumps output this:

string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1) 
string(10) "depth comp" int(0) int(1)

EDIT: @Howdy_McGee in the chat room sent me this code to play with in which he successfully used the depth arg in the start_el method. This code doesn't work for me, as many of the attributes it references (->classes, ->attr_title, ->target, ->xfn, ->url) aren't included in the WP_Post objects I'm getting. How could this be?

EDIT 2: Here's the transcript of the convo I had with @Howdy_McGee in which we narrow down the problem and fail to find a solution.

  • Anyone care to share why the downvote? – Nathan Arthur Jan 7 '16 at 18:55
1

You are passing an argument-- 'depth' => 1,-- that is causing the walker to only pull the first layer of the menu so your depth is going to always be the same value. You've told it to only put the one level.

  • It sounds like the depth restriction is ignored, according to: "However, all levels of the navigation are outputted regardless" – birgire Jan 5 '16 at 8:28
  • @birgire is correct. The depth arg in the walker always evaluates to zero regardless of the item's true depth. – Nathan Arthur Jan 7 '16 at 18:54
  • You are passing an argument instructing the walker to behave in that way. Use var_dump($args->depth); instead. $args is an object. – s_ha_dum Jan 8 '16 at 0:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.