[英]Do something If post has a term from a custom taxonomy
我有一个自定义的分类法“图书馆”,其中有两个术语:“小说”和“小说”
我正在尝试根据我的帖子中的分类法中的术语来执行功能。
因此,如果帖子被分配给“小说”,我需要//do something
,如果帖子被分配给“小说”,我需要//do something else
起初,我一直在尝试使用is_tax
,但这似乎并没有按预期工作:
if ( is_tax('library','fiction' ) ) {
//do something
} elseif ( is_tax('library','novels' ) ) {
//do something else
}
我也尝试过has_term
但这对我也不起作用。
if ( has_term ('fiction','library' ) ) {
//do something
} elseif ( has_term ('novels','libary' ) ) {
//do something else
}
所以我想尝试一下wp_get_post_terms
,但是我不确定该如何执行。
我正在尝试:
$terms = wp_get_post_terms( $post->ID, 'library' );
foreach ( $terms as $term ) {
if($term->name == 'fiction') {
// do something
}
elseif($term->name == 'novels') {
// do something else
}
}
我在那里缺少什么吗? 特别是最后一个?
通过usign get_categories()获取您的类别名称; 功能
$category_args = array(
'type' => 'yourposttypename',
'child_of' => 0,
'parent' => 0,
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'library',
'pad_counts' => false
);
$categories = get_categories( $category_args );
if(count($categories)>0)
{
foreach ( $categories as $category ) {
if($category->name=='fiction')
{
// do something
}
else if($category->name=='novels')
{
// do something else
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.