归根结底,这一切都归结为WP\\U查询,即使您使用get\\U帖子,下面是我的修改版本:
$hasposts = get_posts(\'post_type=sc-events&category=40\');
if( !empty ( $hasposts ) ) {
..// show the drop down menu
}
或
$query = new WP_Query(array(
\'post_type\' => \'sc-events\',
\'category\' => 40
));
if( $query->have_posts() ){
echo \'we have posts\';
} else {
echo \'no posts found\';
}
虽然这会起作用,但有一个受您自己答案启发的替代方案,它使用类别slug而不是其ID:
$term = get_term_by(\'name\', \'whatever category 40 is called\', \'category\');
if($term != false ){
if($term->count > 0 ){
// we have posts
}
}