1

I've custom menu with custom walker and I have custom 'li' elements. But how can I edit parent element?

Example menu

<ul id="some-weirdly-generated-id" data-i-want="some-data-here">
    ...
    <li class="already-custom"> Menu item </li>
    ...
</ul>

I am editing 'li' elements in "start_el(..)" fcion of my worker ...

public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
        $indent = ($depth) ? str_repeat("\t", $depth) : '';      
        $li_attributes = '';
        $class_names = ''`
        ...
        /* I AM EDITING MY LI ELEMENT HERE */
}

But where the hell I can edit "UL" element? Tyvm for tips!

1 Answer 1

0

For the top most ul output by wp_nav_menu(), you can change the entire wrap of of the list items (ul or otherwise) with items_wrap arg.

wp_nav_menu( array(
    ...
    'walker'  => new Your_Nav_Walker_Class(),
    'items_wrap' => '<ul class="your-class" data-u-want="some-data-you-want">%3$s</ul>',
    ...
));

User example on Codex


For uls output from within nav walker (sub level menus, etc.), you can use something like:

function start_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "\n$indent\n";
}
function end_lvl( &$output, $depth = 0, $args = array() ) {
    $indent = str_repeat("\t", $depth);
    $output .= "$indent\n";
}

Altering $depth to target submenus at different levels.

Nav_Walker boilerplate on github

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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