你可以试试这个,在所有wp_nav_menu
项目,使用对象ID获取帖子,然后测试内容是否为空。IF 该内容为空,然后将其从items数组中取消设置:
function remove_empty_items( $items ) {
$blog_page = get_option( \'page_for_posts\' );
$home_page = get_option( \'page_on_front\' );
$postArr = get_posts ( \'post_type=post\' );
foreach( $items as $key => $item ) {
$tmpPost = get_post( $item->object_id );
if( $item->object_id == $blog_page && empty( $postArr ) ) {
unset( $items[$key] );
} else if( $item->object_id !== $home_page && empty( $tmpPost->post_content ) ) {
unset( $items[$key] );
}
}
return $items;
}
add_filter( \'wp_get_nav_menu_items\', \'remove_empty_items\' );