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?
There is no
depth
argument forget_categories()
, you should try :Read more : http://codex.wordpress.org/Function_Reference/get_categories#Get_only_top_level_categories
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
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.