Advertisement
Guenni007

nav-menu-snippet

Apr 22nd, 2022 (edited)
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. // removes Menu-Items if they got no posts - tested with portfolio and posts
  2. // put it into child-theme functions.php
  3.  
  4. function remove_empty_category_menu ( $items, $menu, $args ) {
  5.     if ( ! is_admin() ) {
  6.         global $wpdb;
  7.         $nopost = $wpdb->get_col( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE count = 0" );
  8.         foreach ( $items as $key => $item ) {
  9.             if ( ( 'taxonomy' == $item->type ) && ( in_array( $item->object_id, $nopost ) ) ) {
  10.                 unset( $items[$key] );
  11.             }
  12.         }
  13.     }
  14.     return $items;
  15. }
  16. add_filter( 'wp_get_nav_menu_items', 'remove_empty_category_menu',10, 3 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement