11

私はこのコードを使用しています:

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

$categories = get_categories($args);

私がやろうとしているのは、トップレベルのカテゴリのみをリストすることです。このコードを使用すると、レベル1だけでなくすべてのコードを取得できます。誰かが私を助けることができますか?

4

4 に答える 4

30

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

$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]

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

于 2013-02-27T13:42:56.777 に答える
2

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

$args = array(
'taxonomy' => 'categories',
'parent' => 7,
'hide_empty' => 0,
);
于 2013-08-22T17:51:43.467 に答える
2

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);
?>
于 2017-04-23T03:16:34.900 に答える
0

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

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 />";
                }
            }
        }
    }
}
于 2019-09-21T13:41:12.353 に答える