Skip to content

Instantly share code, notes, and snippets.

@campusboy87
Last active October 10, 2018 00:15
Show Gist options
  • Select an option

  • Save campusboy87/fc03f4e5ccda18ae177be4993144cdfb to your computer and use it in GitHub Desktop.

Select an option

Save campusboy87/fc03f4e5ccda18ae177be4993144cdfb to your computer and use it in GitHub Desktop.
Removes the terms with no posts from the menu.
<?php
add_filter( 'wp_nav_menu_objects', 'delete_empty_terms_from_menu', 10, 2 );
function delete_empty_terms_from_menu( $items, $args ) {
// Проверяем, что область меню primary, чтобы не обрабатывать лишние меню
if ( $args->theme_location === 'primary' ) {
foreach ( $items as $key => $item ) {
// Проверяем, что пункт меню относится к термину и что у него нет постов, и удаляем таковые.
if ( 'taxonomy' == $item->type && get_term( $item->object_id )->count === 0 ) {
unset( $items[ $key ] );
}
}
}
return $items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment