WordPress:从the_category()隐藏未分类的类别

时间:2017-02-24 09:26:37

标签: php wordpress

我使用the_category()功能显示类别列表。但是也列出了uncategorized类别。

如何将其从此处排除?

我无法删除或重命名此类别,因为我需要在其他位置使用它。

4 个答案:

答案 0 :(得分:1)

好的,找到了解决这个问题的好方法......

我给每个类别一个类,并通过css隐藏这个类。

在这里:https://wordpress.stackexchange.com/a/91260

答案 1 :(得分:0)

尝试使用过滤器get_the_category()代替the_category()

add_filter('get_the_categories', 'exc_cat');

function exc_cat($cats) {
        //not on admin pages
        if(!is_admin()){
           $exc = array('uncategorized');
            foreach ($cats as $i=>$cat){
                if(in_array($cat->name, $exc)){
                   unset($cats[$i]); 
                }
            }
        }
    return $cats;
    }

答案 2 :(得分:0)

我已经尝试过了,并且有效。

if((get_the_category_list() !== 'Uncategorized')){
                            the_category();
                        }

答案 3 :(得分:0)

我来晚了,但我想我找到了一个对我有用的好解决方案。

foreach($categories as $category) {
    if ($category->name == "Uncategorized") {
        continue;
    }