How to add post count to wp_nav_menu?

Is it possible to add post count to menu links using wp_nav_menu?

For example:-

    Apple (3)
    ball (6)
    cat (2)

Related posts

2 comments

  1. Basically the easiest thing should be a callback to the start_el() method of the Walker class.

    // From core:
    apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    

    Just grab the data (whatever the actual data is and from wherever it comes) and append it.

    add_action( 'walker_nav_menu_start_el', 'wpse_10042_nav_menu_post_count', 10, 4 );
    function wpse_10042_nav_menu_post_count( $output, $item, $depth, $args )
    {
        // Check $item and get the data you need
        printf( '<pre>%s</pre>', var_export( $item, true ) );
        // Then append whatever you need to the $output
        $output .= '';
    
        return $output;
    }
    
  2. Have you seen this thread?

    $menu_to_count = wp_nav_menu(array(
        'echo' => false,
        'theme_location' => 'menu'
        ));
    $menu_items = substr_count($menu_to_count,'class="menu-item ');
    echo $menu_items;
    

Comments are closed.