下面是另一种使用get_term_field()
:
$count = get_term_field( \'count\', 17, \'post_tag\' );
if( is_int( $count ) && $count > 0 )
{
// tag contains posts
}
我们检查输出是否是大于零的整数。
然后,我们可以创建自定义包装:
/**
* Check if a term has any posts assigned to it
*
* @param int|WP_Term|object $term
* @param string $taxonomy
* @return bool
*/
function wpse_term_has_posts( $term, $taxonomy )
{
$count = get_term_field( \'count\', $term, $taxonomy );
return is_int( $count ) && $count > 0;
}
用法示例:
if( wpse_term_has_posts( 17, \'post_tag\' ) )
{
// do stuff
}