Wordpress: show only TOP level categories

I use 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'm trying to do is list only top-level categories. When I use this code, I get all of them not only at level one. Can anybody help me?

+10
wordpress
source share
4 answers

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

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

parent : (integer) Display only those categories that are direct descendants (that is, only for children) of the category identified by its identifier. This does NOT work as the parameter "child_of". There is no default value for this parameter. [In 2.8.4]

More details: http://codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories

+27
source share

The soju post is very useful because to get a subcategory of only category 1 category you just need to pass the category identifier with subcategories. But if the subcategory has no record, then it does not show, but the subcategory of the subcategory consists of a message, so add "hide_empty" => 0, in the above state it will look like

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

Here is my script to get top level category names from a loop. This will include top-level categories that only have a child category and are not explicitly checked.

 <?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); ?> 
0
source share

This function allows you to select a category level ... so in your case you would choose level 0 and it would look like <?php display_cat_level(0,true); ?> <?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 />"; } } } } } 
0
source share

All Articles