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
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!
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>';
?>
精彩评论