WordPress中是否可以测试一个空术语或类别? [英] Is it possible in WordPress to test for an empty term or category?
问题描述
我有一个项目,要求我列出每种自定义帖子类型的可用条款,并通过css / javascript直观地指出哪些条款/类别为空。有没有一种方法可以返回术语/类别列表并说将一个类添加到空的术语/类别中?谢谢您的协助。
I have a project that requires me to list out the available terms for each custom post type and indicate visually which of the terms/categories are empty via css/javascript. Is there a way to return a list of terms/categories and say add a class to the empty ones ? Thanks for any and all assistance.
推荐答案
是的。首先,您使用 get_terms()来获取您的条款(我假设您的cpt具有与其相关的分类法)
Yes there is. First you get your terms using get_terms() (I'm assuming your cpt has associated taxonomy with it)
<?php
$custom_terms = get_terms('my_taxonomy');
if (is_array($custom_terms) && !empty($custom_terms)) {
# code what you want here...
} else{
# code if your terms come empty...
}
这应该做到。
编辑
在 $ custom_terms
变量之后执行a print_r($ custom_terms);
来查看变量的含义。您应该得到一个填充有 stdClass对象
s的数组,该分类中的每个类别一个。
After $custom_terms
variable do a print_r($custom_terms);
to see what the variable holds. You should get an array filled with stdClass Object
s, one for each category in this taxonomy.
因此,您可以进一步执行以下操作:
So you can further do something like this:
foreach ($custom_terms as $term) {
if ($term->count != 0) {
print_r($term->name);
}
}
这将向您显示非空类别的名称分类法。
This will show you names of non empty categories in your taxonomy.
这篇关于WordPress中是否可以测试一个空术语或类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!