I had an error in the above code. $key used in both for loops.
Working code for filtering empty product categories out of menu items:
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 id is in the non-empty categories array
foreach ( $non_empty_categories as $key2 => $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 );
Related Posts:
- Reflect nested categories in appearance > menus
- Display WooCommerce product categories in a 4 columns custom menu [closed]
- Add Product categories to WordPress menu without losing hierarchy
- Building Menu from Woocommerce Product Categories
- List a maximum of ‘x’ product categories with qty of ‘y’ in each column
- Woocommerce filter function not updating
- Show different menu on each woocommerce category page
- How to remove an item from a custom Walker_Nav_Menu
- How to add all subcategories as submenu in WordPress menu
- WordPress category title not update in navigation menu
- How can I add a Categories page link to a menu? [closed]
- Update wordpress menu with woocommerce products
- Adding 400 product categories with their hierarchy to a navigation menu
- WordPress list categories dropdown with parent-child relation and child under it’s parent
- Do not show sub categories in the loop on archive-product.php
- List just subcategory and products of active category page in Woocommerce
- Display all product tags associated with a specific product category
- why get_the_post_thumbnail( the_ID()) echos extra post id
- How to access deleted term inside delete_product_cat action
- Apply WordPress function on specific woocommerce product categories only
- Filter WooCommerce archive pages by an additional category
- Grandchild Term Things Grandfather Term is It’s Parent
- Searching for a category returns nothing if category is empty
- Custom tax_query filter not working for Woocommerce product categories
- function query_posts disabling current_page_menu class
- How do order product categories – on a parent category page – in Woocommerce?
- Adding custom ID field to the categories
- How can i change what one category displays?
- Remove any product that is featured from regular display loop [WooCommerce]
- Two Sections on Woocommerce Catergory Page [closed]
- Woocommerce search form with category select
- How to display product with two condition, category and brand taxonomy
- wp_list_categories not excluing multiple ids
- Theme for subcategories
- Same menu for different taxonomies to reach different content
- Multiple categories for one menu item?
- How to get category id’s which are added in main menu?
- How to Sync Menu, Widgets and other masters from Main Website to its Sub Site
- Template for product-category page [closed]
- Link from navbar to categories
- How to exclude posts from category posts count
- Exclude category from foreach loop
- Have posts that belong to multiple categories, exclude some categories from homepage
- Exclude all subcategories in the_category (post)
- Pagination doesn’t work on multiple categories
- Theme modification for custom category menus
- I need to exclude from a query a category and a few custom taxonomies
- Exclude categories by ID
- Change appearance based on category but post is in two main categories
- Homepage custom recent news
- wp_nav_menu not appearing correctly on category page
- Exclude the category in WordPress
- wp_list_pages() not showing on posts
- Is custom menu name and category name linked in wordpress?
- How can I exclude a category from the main loop with the category name & not ID?
- Custom menu with categories and tags
- User level categories
- Exclude category from drop down list form
- Global navigation in multisite: problem with categories
- How to automatically generate custom menu item from categories?
- WooCommerce shared categories
- WP-CLI Add category as menu item main-menu
- displaying the last product comment in the woocommerce category
- Displaying Single Product Page Parent and Child Category
- exclude certain categories form archive widget
- Woocommerce only display child category when parent is checked (on add new product page)
- Categories are not available AT ALL under Menus
- category description not showing in my woocommerce
- Conditional label Woocommerce archive [unsolved]
- Show audio player only in specific post type category
- How to make product image inherit from category image
- How to show given products only using product id array from specific category page in woocommerce
- Shop Category Pages missing s
- How to set acf color field as background color to product category
- How to target all woocommerce categories that don’t have any subcategories in them?
- WooCommerce REST API not returning the default products category
- In if…else condition, the else statement shows even if if statement is correct
- How to show WooCommerce Categories on ‘shop’ page instead of products?
- Show the parent taxonomy by creating shortcode in woocommerce?
- How can I display all products from all categories on one page with woocommerce?
- ADD DYNAMIC attribute for category to accordion
- Exclude a category and post_type from wp_query
- How to edit woocommerce sub-category page
- When creating a new product, auto assign it to all custom taxonomy woocommerce
- Display menu in category and sub category
- Get WooCommerce product category list in functions.php
- in post content shortcode works, but hardcoded in same page template doesn’t?
- Is there a reason to put a product in both the Child and Parent Categories?
- Function add class to second menu specific items if the page has a certain category
- Dynamically display sub-categories on primary menu without adding them
- Show alternative menu based on page template AND post category
- WooCommerce showing active product categories on single product page
- selecting categories that have products with tag/category
- Restrict retrieved terms by category?
- How to fix 404 error in pagination in categories of woocommerce with common url base /shop/?
- How to display custom fields as table in Woocommerce
- How do I merge WooCommerce categories with the same name?
- Woocommerce Category Sort Dropdown
- Woocommerce: all products of all categories instead products of one category
- How can I exclude a particular category from my WordPress Page 1 and Page 2?