而不是使用get_category()
或任何直接呼应的方法,尝试使用get_the_category()
, 它只返回一个类别数组。然后可以旋转这些项并检查它们是否为空()。
可能类似于:
$categories = get_the_category( $post->ID );
if( is_array(categories) && count($categories) > 0 ) {
foreach( $categories as $category ) {
$categories_string .= $category . ", ";
}
$categories_string = rtrim(", ", $categories_string);
}
if( !empty( $categories_string ) ) {
echo "Posted 4 hours ago in {$categories_string} by ...";
} else {
echo "Posted 4 hours ago by ...";
}
*未经测试