我有这个问题:
$cat_array = array();
$args=array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'exclude\' => \'187\',
\'posts_per_page\' => 19,
\'caller_get_posts\'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$cat_args=array(\'orderby\' => \'none\');
$cats = wp_get_post_terms( $post->ID , \'category\', $cat_args);
foreach($cats as $cat) {
$cat_array[$cat->term_id] = $cat->term_id;
}
endwhile;
}
if ($cat_array) {
foreach($cat_array as $cat) {
$category = get_term_by(\'ID\',$cat, \'category\');
echo \'<li><a href="\' . esc_attr(get_term_link($category, \'category\')) . \'" title="\' . sprintf( __( "View latest posts in %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a>\'.\'</li>\';
}
}
wp_reset_query();
其中列出了最新更新的类别。一切都很好。但我想从那里的列表中排除一个特定类别。这可以像我在查询中一样完成
\'exclude\' => \'187\',
但问题在于以下示例:我有一篇文章属于两个类别,一个类别是我不想显示的类别,另一个类别是我想显示的类别。如果我像上面所说的那样简单地排除它,那么列表中不会显示任何类别,因为它将完全排除该帖子。我怎样才能显示其他类别,并隐藏我不想显示的类别?
有什么线索吗?
谢谢
最合适的回答,由SO网友:vyperlook 整理而成
solved this with
if ($cat_array) {
foreach($cat_array as $cat) {
$category = get_term_by(\'ID\',$cat, \'category\');
if($category->name != \'Excluded category\'){
echo \'<div class="catspace"><a href="\' . esc_attr(get_term_link($category, \'category\')) . \'" title="\' . sprintf( __( "latest posts %s" ), $category->name ) . \'" \' . \'>\' . $category->name.\'</a>\'.\'</div>\';
}else{echo \'<div class="catspace">\'. \'\' .\'</div>\'; }
}
}