Last active
April 27, 2016 18:31
-
-
Save tharmann/c539a30e6fdec2a2d87446aff0e18377 to your computer and use it in GitHub Desktop.
Append count in parentheses to each menu item on menu of WooCommerce Product Categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//add the count to our shop menu items | |
function nim_menu_item_count( $items, $menu, $args ) { | |
// Only proceed if we are looking at the correct menu and not in the admin | |
if ( $menu->slug != 'your-menu-slug' || is_admin() ) | |
return $items; | |
//grab each menu item and it's object_id (which ends up being the category ID for the product category) | |
foreach ($items as $item) { | |
$cat_terms = get_terms( 'product_cat', array( 'include'=>$item->object_id ) ); | |
//tack the count on to the title if it's not zero | |
if ( $cat_terms[0]->count != 0 ) | |
$item->title .= '('.$cat_terms[0]->count.')'; | |
} | |
return $items; | |
} | |
add_filter( 'wp_get_nav_menu_items', 'nim_menu_item_count', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment