Skip to content

Instantly share code, notes, and snippets.

@gaelbillon
Created December 1, 2020 23:03
Show Gist options
  • Save gaelbillon/7fc45853cd7ab7a5f0b5163135c8d028 to your computer and use it in GitHub Desktop.
Save gaelbillon/7fc45853cd7ab7a5f0b5163135c8d028 to your computer and use it in GitHub Desktop.
Hide empty categories in menus
//Hide empty categories in menus
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment