虽然我在谷歌上做了一些搜索,但我没有找到这个问题的答案:有没有办法检查一篇文章是否属于只有1篇文章的类别(该类别中是否只有一篇)?我需要在上执行此检查single.php
.
if ( \'the_magic_check\' ) {
// do something
}
谢谢!虽然我在谷歌上做了一些搜索,但我没有找到这个问题的答案:有没有办法检查一篇文章是否属于只有1篇文章的类别(该类别中是否只有一篇)?我需要在上执行此检查single.php
.
if ( \'the_magic_check\' ) {
// do something
}
谢谢!使用get_the_category
获取属于该职位的类别。然后您可以使用$category->count
返回附加到该职位的特定类别的职位数量
示例:
如果一篇文章只附加了一个类别,您可以执行以下操作
$category = get_the_category();
echo $category[0]->count;
在检查特定类别中是否只有一个帖子,然后做一些事情,您可以尝试if( 1 == $category[0]->count ) {
// Do something if the category has one post only
}
通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问