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 );