skip to Main Content

You can of course add unlimited WooCommerce Product Categories into your main navigation. But sometimes they get empty because of Stock change or similar.

If you automatically want to hide empty product categories in your WordPress Navigation place the code below into your functions.php file.

add_filter( 'wp_get_nav_menu_items', 'nav_remove_empty_category_menu_item', 10, 3 );
function nav_remove_empty_category_menu_item ( $items, $menu, $args ) {
    global $wpdb;
    $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
    foreach ( $items as $key => $item ) {
        if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
            unset( $items[$key] );
        }
    }
    return $items;
}

Daniel Barenkamp

Founder and Creator of WeLaunch (formerly DB-Dzine).

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

×Close search
Search