I am working in WooCommerce and trying to filter out the empty sub categories. I found this code, which filters out everything "empty"...including my Home page link, About us link, etc.
function exclude_empty_cat_menu_items( $items, $menu, $args ) {
// Get a list of product categories that excludes empty categories
$non_empty_categories = get_categories(array('taxonomy' => 'product_cat'));
// Iterate over the menu items
foreach ( $items as $key => $item ) {
$is_empty = true;
// check current item is in the non-empty categories array
foreach ( $non_empty_categories as $key => $cat )
if ($item->title == $cat->name)
$is_empty = false;
// if it is empty remove it from array
if ($is_empty) unset($items[$key]);
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'exclude_empty_cat_menu_items', null, 3 );
And here is my menu...
<?php
$args = array(
'theme_location' => 'lower-bar',
'depth' => 0,
'container' => false,
'fallback_cb' => false,
'menu_class' => 'nav navbar-nav',
'walker' => new BootstrapNavMenuWalker()
);
wp_nav_menu($args);
?>
I'm not married to the idea of using the WordPress menu this way. I normally hard code it in. Is there any way I can accomplish showing all parent categories and pages while filtering out empty child categories?