admin管理员组

文章数量:1122832

Hello, I integrated this same code to hide a category when it is empty and it works well. But I would like it to be positioned in the same place when a product is added again. Today, it is positioned at the very bottom of the menu. Do you have a solution to hide this category without it moving? Thanks for your help ?

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;
}

Hello, I integrated this same code to hide a category when it is empty and it works well. But I would like it to be positioned in the same place when a product is added again. Today, it is positioned at the very bottom of the menu. Do you have a solution to hide this category without it moving? Thanks for your help ?

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;
}
Share Improve this question edited Jun 24, 2024 at 15:07 Chris Cox 2,2661 gold badge13 silver badges22 bronze badges asked Jun 24, 2024 at 13:42 Val MerVal Mer 1 8
  • I can see nothing in that code is moving the menu item, are you sure the reason it's showing at the end is because it's at the end of the menu? How is it being inserted? Note this is an X Y problem, it might be easier to ask how to hide empty categories in a menu than it would be to ask how to fix this code – Tom J Nowell Commented Jun 24, 2024 at 15:09
  • in fact, this code allows me to hide an empty category. The problem is that when this same category fills up again, it is automatically placed at the bottom of the menu in "Appearance > Menu. I would like it not to move in position. Can you help me? – Val Mer Commented Jun 25, 2024 at 12:28
  • I understand that, but you did not answer any of my questions, are you manually adding the categories to the nav menu? Is code or a plugin adding the categories? The filter you're trying to fix is not hiding items, it's deleting them. Where did you get the code from? – Tom J Nowell Commented Jun 26, 2024 at 12:12
  • actually, I manually add the categories in Appearance > Menu. The code was found on the net and added to function.php of my child theme. – Val Mer Commented Jun 27, 2024 at 12:27
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Jun 28, 2024 at 19:55
 |  Show 3 more comments

1 Answer 1

Reset to default 1

I think with your question to hide the empty product categories in menu, we can add extra class for this menu item then adding CSS to hide these specific items.

  1. This code will add extra class hidden to empty product categories
add_filter('nav_menu_css_class', function($classes, $menu_item, $args, $depth){
    if('taxonomy' == $menu_item->type){
        $term_id = $menu_item->object_id;
        $term    = get_term($term_id, $menu_item->object);
        if(!is_wp_error($term)){
            if($term->count <= 0){
                $classes [] = 'hidden';
            }
        }
    }
    return $classes;
}, 10, 4);
  1. Then adding new CSS to hide these items
    .hidden {display:none;}

本文标签: Hide product categories if empty by leaving them positioned in the same place in the menu