Guenni007 icon

nav-menu-snippet

Guenni007 | PRO | 04/22/22 09:55:52 AM UTC (Edited) | 0 ⭐ | 1529 👁️ | Never ⏰ | []
PHP |

640 B

|

None

|

0 👍

/

0 👎

// removes Menu-Items if they got no posts - tested with portfolio and posts
// put it into child-theme functions.php
 
function remove_empty_category_menu ( $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;
}
add_filter( 'wp_get_nav_menu_items', 'remove_empty_category_menu',10, 3 );

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎