Search code examples
wordpressmenuparentmenuitemchildren

Add amount of children to parent page menu item in Wordpress


I have a menu with 10 parent pages in Wordpress. Each parent page have alot of children (sub-pages). I would like to append to each menu item a number showing how many children that item has.

EXAMPLE:

Standard menu: Products | Portfolio | Store | Jobs

Menu i want: Products (49) | Portfolio (14) | Store (22) | Jobs (51)

This indicates that the parent page "Products" has 49 children. "Portfolio" has 14 etc. Adding new sub-pages to these parent pages would automatically update the number.

I am using the default menu that wordpress creates of your pages, and not the menu-system found under apperance. Using TwentyThirteen theme.

Anyone?

Anders


Solution

  • In such case you can use the walker. Put the following class in your functions.php file

    class themeslug_walker_nav_menu extends Walker_Nav_Menu {
    
        // add main/sub classes to li's and links
        function start_el( &$output, $item, $depth, $args ) {
            global $wp_query;
            $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
    
            // depth dependent classes
            $depth_classes = array(
                ( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
                ( $depth >=2 ? 'sub-sub-menu-item' : '' ),
                ( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
                'menu-item-depth-' . $depth
            );
            $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
    
            // passed classes
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
    
            // build html
            $output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
    
            // link attributes
            $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        ) .'"' : '';
            $attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
            $childs = count(get_pages('child_of='.$item->object_id.'&parent='.$item->object_id));
            $item_output = sprintf( '%1$s<a%2$s>%3$s%4$s (%5$s) %6$s</a>%7$s',
                $args->before,
                $attributes,
                $args->link_before,
                apply_filters( 'the_title', $item->title, $item->ID ),
                $childs,
                $args->link_after,
                $args->after
            );
    
            // build html
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
        }
    }
    

    and add this walker in your wp_nav_menu function

    <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'walker' => new themeslug_walker_nav_menu ) ); ?>

    That's it.