Scenario: Clients can add or remove posts at will, but may not be comfortable (or even bother) adding categories to a menu.
Problem: This can cause empty categories to be displayed in menus.
Question: Is there a core function or resource that can be called from functions.php that will cause empty categories not to show on the front side even if they are showing as added in the dashboard.
Previous research: I have searched in both Google and this StackExchange for an answer. I may not be finding it by using the wrong search terms.
Scenario: Clients can add or remove posts at will, but may not be comfortable (or even bother) adding categories to a menu.
Problem: This can cause empty categories to be displayed in menus.
Question: Is there a core function or resource that can be called from functions.php that will cause empty categories not to show on the front side even if they are showing as added in the dashboard.
Previous research: I have searched in both Google and this StackExchange for an answer. I may not be finding it by using the wrong search terms.
Share Improve this question asked Apr 11, 2018 at 19:19 Nora McDougall-CollinsNora McDougall-Collins 3952 silver badges15 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 5
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;
}
Please use the above code, it will unset the category which has No post. Assuming that you were are managing Menu from Apperance->Menu.
Hope that helps :)
It's a fairly old tread but using @Aishan's function in a similar context I faced an issue.
Without the is_admin()
condition before the exclusion of the empty categories from the menu items, these items would be excluded from the menu edit in the wp-admin. That means that they would be deleted from the menu if anyone save the menu while these categories are empty.
In my specific case I needed the menu items to remain there while just being filtered on the front if empty. If the category become not empty again, the item will then reappear in the menu.
Anyway here is my edited version
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;
}
wp_nav_menu
s? If so, I'd suggest building more widgets into the site rather than strictly menus. That way for example in a blog sidebar you could add a menu widget which will automatically hide empty categories and also automatically add any new categories they create. – WebElaine Commented Apr 11, 2018 at 19:21