Hide empty categories / menu items from dropdown menu not working correctly

  • Unknown's avatar

    Good day.

    I know a similar topic was posted before, and i’ve followed those, but am now experiencing an issue I need some assistance please.

    I’ve added a code in the functions.php file of the blocksy child theme which I still use as the active theme on my wordpress site. The site is an e-commerce site.
    (And no, the support team for Blocksy cannot help with custom code)

    The code is used to hide the empty product categories from the menu on the front-end.

    On this site, the product categories are all set as sub-items under a dropdown menu named “Categories”

    The code works, however, it is show a specific Category or Menu item, outside of the dropdown menu as duplicate.

    I’ve done the following troubleshooting:

    Recreate the menu item
    Move the menu item to a different location in the menu
    Removed the category from it’s parent category
    Renamed the category and menu item

    None of the above resolves the issue.

    The code used is as follows:

    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 ) {
    if ( ! is_admin() ) {
    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;
    }

    Thanks in advance

    The blog I need help with is: (visible only to logged in users)

  • Hi there!

    The website you need help with, techstreme.co.za is not hosted on WordPress.com. With this in mind, I encourage you to reach out to the WordPress.org forum community and open a thread there or to the current theme developers:

    https://wordpress.org/support/forums/

    Support

    Please note that WordPress.com and WordPress.org, despite similar in name, are not the same platform and we wrote a more in-depth guide about it here:

    WP
    WordPress.com vs. WordPress.org
    6 min read
    WordPress powers millions of websites, from bloggers and small businesses to massive news sites and companies. This guide will help you to understand the difference between WordPress.com and WordPress.org, and which might be the best fit for your website. Overview of Differences The same WordPress software powers both WordPress.com and WordPress.org sites. One of the main differences is ho

    Warm regards,

  • The topic ‘Hide empty categories / menu items from dropdown menu not working correctly’ is closed to new replies.