0

I'm trying to show WooCommerce Product Categories, Post Categories, Product Tags, Post Tags and Custom Taxonomy posts count in main navigation using wp_nav_menu.

My WP_NAV_MENU code

                <?php if( has_nav_menu( 'primary_menu' ) ) : ?>
                    <?php
                        $primary_args = array(
                            'theme_location'    => 'primary_menu',
                            'echo' => false,
                            'depth'             => 3,
                            'container'         => false,
                            'menu_class'        => 'header-menu',
                            'menu_id'           => 'header-menu'
                        );
                        wp_nav_menu( $primary_args );
                    ?>
                <?php endif; ?>

I added the following code into my child-theme functions.php

apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
add_action( 'walker_nav_menu_start_el', 'menu_post_count', 10, 4 );
function 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;
}

This caused error on site showing the data of the category as following.

WP_Post::__set_state(array(
   'ID' => 153,
   'post_author' => '1',
   'post_date' => '2015-12-22 21:52:07',
   'post_date_gmt' => '2015-12-22 19:52:07',
   'post_content' => ' ',
   'post_title' => '',
   'post_excerpt' => '',
   'post_status' => 'publish',
   'comment_status' => 'closed',
   'ping_status' => 'closed',
   'post_password' => '',
   'post_name' => '153',
   'to_ping' => '',
   'pinged' => '',
   'post_modified' => '2015-12-29 08:47:30',
   'post_modified_gmt' => '2015-12-29 06:47:30',
   'post_content_filtered' => '',
   'post_parent' => 0,
   'guid' => 'http://domain.com/?p=153',
   'menu_order' => 1,
   'post_type' => 'nav_menu_item',
   'post_mime_type' => '',
   'comment_count' => '0',
   'filter' => 'raw',
   'db_id' => 153,
   'menu_item_parent' => '0',
   'object_id' => '39',
   'object' => 'page',
   'type' => 'post_type',
   'type_label' => 'page',
   'url' => 'http://domain.com/',
   'title' => 'homepage',
   'target' => '',
   'attr_title' => '',
   'description' => '',
   'classes' => 
  • That doesnt look like an error to me. It's doing exactly what you told it to. var_export will the data assigned to that variable. – ngearing Jun 15 '16 at 6:28
  • @Nath I found this code some place here please advise the best code edited to end with the requested result? And if you have other suggestion , thank you! – WiTon Nope Jun 15 '16 at 11:58
  • Do you have the post categories / tags / taxonomies added to you menu already?? – ngearing Jun 15 '16 at 23:27
  • @Nath Yes I do have them using the Appearance > Menu – WiTon Nope Jun 22 '16 at 20:10
1

Alright so this isn't too difficult, first we will just check if the menu item is a taxonomy then get the count and display it!

function ggstyle_menu_item_count( $output, $item, $depth, $args ) {
    // Check if the item is a Category or Custom Taxonomy
    if( $item->type == 'taxonomy' ) {
        $object = get_term($item->object_id, $item->object);

        // Check count, if more than 0 display count
        if($object->count > 0)
            $output .= "<span class='menu-item-count'>".$object->count."</span>";
    }    

    return $output;
}
add_action( 'walker_nav_menu_start_el', 'ggstyle_menu_item_count', 10, 4 );

Edit

To get the Item count to output into the <a> of the menu item we will have to split up the $output and insert our content the put it back together.

function ggstyle_menu_item_count( $output, $item, $depth, $args ) {
    // Check if the item is a Category or Custom Taxonomy
    if( $item->type == 'taxonomy' ) {
        $object = get_term($item->object_id, $item->object);

        // Check count, if more than 0 display count
        if($object->count > 0) {
            $output_new = '';
            $output_split = str_split($output, strpos($output, '</a>') );
            $output_new .= $output_split[0] . "<span class='menu-item-count'>".$object->count."</span>" . $output_split[1];
            $output = $output_new;
        }
    }    

    return $output;
}
add_action( 'walker_nav_menu_start_el', 'ggstyle_menu_item_count', 10, 4 );
| improve this answer | |
  • @Naith thank you for your reply. Should I only add this into the child functions.php anything required to be done at wp_nav_menu level? – WiTon Nope Jun 24 '16 at 0:12
  • Yeah just in your functions. The action it hooks into 'walker_nav_menu_start_el' is part of the wp_nav_menu function. – ngearing Jun 24 '16 at 0:44
  • I've applied the code thanks a lot it's working fine however it has messed up with the style thus is it possible to embed the count into the <a> tag rather then an individual span to be linkable and fits with the site style. Thanks again! – WiTon Nope Jun 25 '16 at 0:45
  • Sure, I've updated my answer. – ngearing Jun 27 '16 at 0:18

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.