0

I have three levels of my menu created in custom walker class. Output is like below:

  • home (parent)
  • about (parent)
    • item 1 (level 1 child)
      • item 1-1 (level 2 child)
      • item 1-2 (level 2 child)
  • services (parent)
    • item 1 (level 1 child)
      • item 1-1 (level 2 child)
      • item 1-2 (level 2 child)
    • item 2 (level 1 child)
      • item 2-1 (level 2 child)
      • item 2-2 (level 2 child)

I want to count only "level 1 childs" inside start_lvl() for each parent item individually.

In above example, output should be for about (1) and services (2) because "about" has only 1 child for level 1, and services has 2 childs for level 1

Please somebody help, I have searched a lot and tried almost all methods available on Stack Exchange, Overflow etc...

  • You're gonna need to show us the code you are using; and what part exactly didn't work. – josh Jun 9 '15 at 22:45
  • Everything works, just unable to create proper logic to get counted values for childs of level 1 i.e. for depth 1 and each counted value should call individually for its relevant parent item as illustrated in original post. – Adeel Jun 9 '15 at 22:48
0

I'm trying to make my logic like this:

calling below code inside

function start_el( &$output, $item, $depth, $args ) {
$this->sub_items = 0; // declared variable as private $sub_items = 0 above inside class 
            if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) ) {
                $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
                $menu_items = wp_get_nav_menu_items($menu->term_id);
                foreach( $menu_items as $menu_item ) {

                        if( $menu_item->menu_item_parent == $item->ID ) {
                            $this->sub_items++; 
                        }
                }
           } 
}

I'm asuming that each time $this->sub_items returns calculated value for first level childs, and I can access it inside

function start_lvl( &$output, $depth ) {
     echo $this->sub_items;
}

But unfortunately it doesn't output correctly :(

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.