0

I am trying to extend the Walker_Nav_Menu walker to make some changes to the way WP outputs menus. The method I have to override is start_el, so I created my Walker and copied the base method:

<?php
class myCustom_Walker_Nav_Menu extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth, $args) {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

        $class_names = $value = '';

        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
        $class_names = ' class="' . esc_attr( $class_names ) . '"';

        $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
        $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';

        $output .= $indent . '<li' . $id . $value . $class_names .'>';

        $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        ) .'"' : '';

        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}  
?>

My problem is that I dont seem able to get the children for the current item ($item). Can somebody help me?

0

count (get_children($item));

Does that help?

  • It doesnt work... For some reason it returns 11 for all menu items, even though mymenu has only 5 items in total. Weird – 0al0 Nov 23 '11 at 20:10
  • What does the print_r look like? – AlxVallejo Nov 23 '11 at 21:12
  • I would also try get_pages with arguments of child_of($item). If you also add the parent=$item, it will only go one layer deep. – AlxVallejo Nov 23 '11 at 21:15
0

Found a solution in a very similar question. The accepted solution worked perfectly for me, although it involves fixing a wordpress bug...

Add 'has_children' class to parent li when modifying Walker_Nav_Menu

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.