3

在我的 WordPress 博客中,有一些类别用于内部工作。我想隐藏每个帖子下显示的博客主页类别列表中的类别。

我将类别列表打印为print(the_category($postID));

如何在每个帖子下隐藏或制作该类别列表打印的过滤器?

4

4 回答 4

3

我的解决方案:

.cat-item-1 {display:none;}
于 2013-11-05T15:51:31.710 回答
2

如果要在前端隐藏所选类别,请尝试使用get_the_terms过滤器。也许像这样,尝试将其添加到您的functions.php

add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){

    // list of category slug to exclude, 
    $exclude = array('your-term-slug', 'another-term-to-hide');

    if (!is_admin()) {
        foreach($terms as $key => $term){
            if($term->taxonomy == "category"){
                if(in_array($term->slug, $exclude)) unset($terms[$key]);
            }
        }
    }

    return $terms;
}
于 2015-06-18T04:52:17.473 回答
1

您需要获取帖子的类别,取出您不想要的类别,然后显示。

<?php
$postCats = wp_get_post_categories($post->ID);
$cats =  array();

foreach($postCats as $c){
 $cats[]= get_cat_name($c);
}
$dontShow = array("List","the","Categories","here");
echo implode(", ", array_diff($cats,$dontShow);
?>
于 2012-06-28T14:44:06.007 回答
1
$exclu_categories=array(
    1=>'',
    34 => '',
    45=>'',
);
$categoires=get_the_category();

foreach($categoires as $category) {

   $cat_id=$category->cat_ID;
   $cat_name=$category->name;

   if(!isset($exclu_categories[$cat_id])) {
      echo $cat_name;
   }

}
于 2013-11-05T16:04:36.320 回答