Monday, August 20, 2018

Display categories list in Wordpress without subcategories

I am attempting to show categories in a list, but without displaying subcatgories. My current code is:

cat_ID) != "") {
    echo "
    "; wp_list_categories('orderby=id&show_count=0&title_li= &use_desc_for_title=1&child_of='.$this_category->cat_ID); echo "
"; } }?>

Which displays the categories nicely

enter image description here

but when I added a sub-category it looked like this:

enter image description here

Any ideas? Thanks!

Solved

Use get_categories() instead. https://developer.wordpress.org/reference/functions/get_categories/#Get_only_top_level_categories

It has "parent" parameter which you can set to 0 and get desired result.


No comments:

Post a Comment