Custom nav walker with different output depending on depth

Sounds like you want to check the $depth variable. A depth of 0 is a top level item, a depth of 1 is a child of a top level item, etc.

So in keeping with your current code, you could factor out that logic this way:

$format="%1$s<a%2$s><div>%3$s%4$s%5$s</div></a>%6$s";
if( $depth > 0 ) $format="%1$s<a%2$s><div class="flintstone-nav">%3$s%4$s%5$s</div></a>%6$s";
$item_output = sprintf( $format,
        $args->before,
        $attributes,
        $args->link_before,
        apply_filters( 'the_title', $item->title, $item->ID ),
        $args->link_after,
        $args->after
    );

As a side note, I wouldn’t but a <br> tag inside your anchor, as it’s unnecessary. I’d also recommend you use the core menu item field “Description” to set up your “smaller text”, rather than having to add markup in the Menus panel, which can get messy. The Description field can be revealed in the Screen Options tab, and its value can be accessed via $item->description. Doing it that way will keep things a lot cleaner.

Leave a Comment