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
1 Answer
Reset to default 1I 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.
- 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);
- 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
版权声明:本文标题:Hide product categories if empty by leaving them positioned in the same place in the menu 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300635a1930886.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论