我自己也经历了一场有趣的斗争。
我正在使用http://codex.wordpress.org/Function_Reference/get_the_termshttp://codex.wordpress.org/Function_Reference/is_object_in_term
我必须根据分类标记的存在来显示类别标题和其他元数据。
我用is\\u object\\u in\\u术语检查了一组特定标记,得到了不可预测的结果,但我所做的可能对其他人有用。最后,我使用NULL使其正常工作,但我只需要检查是否存在分类法,以及该分类法下的任何术语是否应用于正在查看的帖子,这样我就可以呼出某些显示元素。
我假设如果你只是在寻找一个术语,那么codex页面上的股票示例应该会有所帮助。类似于。。。
if ( is_object_in_term( $post->ID, \'news\', \'subject\' ) ) :
echo \'YES\';
else :
echo \'NO\';
endif;
我的相关代码如下:
$heading_nicename = array(\'Category\',\'Neighborhoods\',\'Zip Codes\',\'Member Agencies\',\'Nearest Public Transportation\');
$tax_slug = array(\'category\',\'neighborhoods\',\'zip_codes\',\'member_agencies\',\'nearest_public_transportation\');
$heading_key = 0;
$termindex = 0;`
foreach ( $tax_slug as $taxonomy ) {
//used to spit out terms as links
$object_terms = wp_get_object_terms($post->ID, $taxonomy, array(\'fields\' => \'all\'));
//used in a check to see if tax terms even apply to post
$o_terms = get_the_terms( $post->ID, $taxonomy );
if ( $o_terms && ! is_wp_error( $o_terms ) ) {
$check_these_terms = array();
}
foreach ( $o_terms as $term ) {
$check_these_terms[] = $term->slug;
//echo $term->slug.\'<br/>\';
}
$o_list = join( ", ", $check_these_terms );
//echo \'taxonomy: \'.$taxonomy.\'<br/>\';
//echo \'terms: \'.$o_list.\'<br/>\';
//unpredictable results
//if ( is_object_in_term( $post->ID, $taxonomy, array($o_list ) ){
//echo \'<h4>\'.$heading_nicename[$heading_key].\'</h4>\';
//}
//Ends up being a check for ANY term under a taxonomy
if ( is_object_in_term( $post->ID, $taxonomy, null ) ){
echo \'<h4>\'.$heading_nicename[$heading_key].\'</h4>\';
}
$heading_key++;
echo \'<p>\';
$endindex = count($object_terms);
$termloop = 0;
foreach ($object_terms as $term) {
echo \'<a href="\' . esc_attr(get_term_link($term, $taxonomy)) . \'" title="\' . sprintf( __( "View all posts in %s" ), $term->name ) . \'" \' . \'>\' . $term->name.\'</a>\';
//clean up commas
if ($termloop !== ($endindex - 1)){
echo \', \';
$termloop++;
}
$termindex++;
}
echo \'</p>\';
}`