Created
December 1, 2020 23:03
-
-
Save gaelbillon/7fc45853cd7ab7a5f0b5163135c8d028 to your computer and use it in GitHub Desktop.
Hide empty categories in menus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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