views:

329

answers:

1

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

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!

+1  A: 

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>';
?>
remi
can you give me an idea how to apply this? I tried putting it on my theme but nothing happens, I also tried changing the value for my parent category, same nothing happens... thanks!
Pennf0lio
Sure! I updated my answer with a working code example :)
remi