Wordpress:仅显示顶级类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15113247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:10:27  来源:igfitidea点击:

Wordpress: Show only TOP level categories

wordpress

提问by Andrei

I am using this bit of code:

我正在使用这段代码:

$args = array(
  'orderby' => 'name',
  'hierarchical' => 1,
  'style' => 'none',
  'taxonomy' => 'category',
  'hide_empty' => 0,
  'depth' => 1,
  'title_li' => ''
);

$categories = get_categories($args);

What I am trying to do is to list only top level categories. When I am using this code I am getting all of them not just level one. Can someone help me?

我想要做的是只列出顶级类别。当我使用这段代码时,我得到的不仅仅是一级。有人能帮我吗?

回答by soju

There is no depthargument for get_categories(), you should try :

没有depth理由get_categories(),你应该尝试:

$args = array(
  'orderby' => 'name',
  'parent' => 0
);

parent: (integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the 'child_of' parameter. There is no default for this parameter. [In 2.8.4]

parent: (integer) 仅显示由其 ID 标识的类别的直接后代(即仅子项)的类别。这不像 'child_of' 参数那样工作。此参数没有默认值。[在 2.8.4]

Read more : http://codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories

阅读更多:http: //codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories

回答by Bikram Shrestha

soju post is very helpful, for to get category only 1 level subcategory we should just pass the category id that has subcategories . But if the subcategory have no any post then it doesnot shows but subcategory subcategory consist the post so add 'hide_empty' => 0, in the above condition it will look like

soju post 非常有帮助,因为要获得类别只有 1 级子类别,我们应该只传递具有子类别的类别 ID。但是如果子类别没有任何帖子那么它不会显示但是子类别子类别包含帖子所以添加'hide_empty' => 0,在上面的条件下它看起来像

$args = array(
'taxonomy' => 'categories',
'parent' => 7,
'hide_empty' => 0,
);

回答by squarecandy

Here is my script to get top level category names from within the loop. This will include top level categories that just have a child category checked, and are not explicitly checked themselves.

这是我从循环中获取顶级类别名称的脚本。这将包括仅检查子类别且本身未明确检查的顶级类别。

<?php
    $categories = get_the_category();
    $topcats = array();
    foreach ($categories as $cat) {
        if ($cat->parent != 0) $cat = get_term($cat->parent, 'category');
        $topcats[$cat->term_id] = '<a href="/category/' . $cat->slug . '">' . $cat->name . '</a>';
    }
    echo implode(', ', $topcats);
?>

回答by Pete

This function allows you to choose which category level... so in your case you'd choose level 0 and it would look like <?php display_cat_level(0,true); ?>in your single.php theme file

此功能允许您选择哪个类别级别...因此在您的情况下,您将选择级别 0,它看起来像<?php display_cat_level(0,true); ?>在您的 single.php 主题文件中

https://github.com/pjeaje/code-snippets/blob/gh-pages/display%20a%20specific%20category%20level%20of%20a%20post%20inside%20the%20loop

https://github.com/pjeaje/code-snippets/blob/gh-pages/display%20a%20specific%20category%20level%20of%20a%20post%20inside%20the%20loop

// http://wpquestions.com/question/showChronoLoggedIn/id/9333
// display a specific category level of a post inside the loop
// USAGE: <?php display_cat_level(X,true); ?> where TRUE = linked | false/empty = not linked
function get_level($category, $level = 0)
{
    if ($category->category_parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_category($category->category_parent);
        return get_level($category, $level);
    }
}

function display_cat_level( $level = 0 , $link=false){

    $cats = get_the_category( );
    if( $cats ){
        foreach($cats as $cat){
            $current_cat_level = get_level($cat);
            if( $current_cat_level  == $level ){
                if($link==true) {
                    echo '<a href="'.get_category_link($cat->cat_ID).'">'.$cat->name."</a><br />";
                } else {
                    echo $cat->name."<br />";
                }
            }
        }
    }
}