WordPress display specific sub category of a parent category

So here’s the scenario, I’m building a theme that would display sub category of a parent post

for Food:
[Food]
->Hotdog
->Eggs
->Fries

Read More

for Toys:
[Toys]
->Doll
->Car
->Drums

for People:
[People]
->Mom
->Dad
->Uncle

now I don’t want to display their parent category, just their subcategory (eg Doll, Car, Drums). I’ve looked list_cats() and wp_list_categories() but I can’t figure out how to display it right.

Thanks!

Related posts

Leave a Reply

1 comment

  1. You need to use get_categories for that.

    <?php
    $subcategories = get_categories('&child_of=4');
    foreach ($subcategories as $subcategory) {
      // var_dump($subcategory);
    }
    ?>
    

    Update: A more complete example:

    <?php
    $subcategories = get_categories('&child_of=4&hide_empty'); // List subcategories of category '4' (even the ones with no posts in them)
    echo '<ul>';
    foreach ($subcategories as $subcategory) {
      echo sprintf('<li><a href="%s">%s</a></li>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));
    }
    echo '</ul>';
    ?>