问题
I am doing some changes on my blog theme, and i need to list the subcategories when user is inside the parent category, example:
- main-category -- subcat1 -- subcat2
So i need to show the subcat1 and subcat2 only when the user is inside main-category. At this moment i have 2 divs, one for the main-categories (this is what i need users to see on all pages) and one div bellow the main-category div to show the subcategories.
Is this possible? I am sorry for my English, let me know if this is too confuse.
Thanks for your time. Regards
回答1:
Yes, it's possible using get_the_category() and wp_list_categories() with the child_of
parameter. Use something like the following on your main-category page:
$category = get_the_category();
wp_list_categories('child_of=' . $category[0]->cat_ID);
回答2:
try with a function <?php echo list_categories(); ?>
$categories = get_categories($args);
$html = '';
foreach($categories as $cat){
if($cat->category_parent == 0){
$html .= '';<--- your code
$childCategories = get_categories('child_of='.$cat->cat_ID.'');
if(!empty($childCategories)){
foreach($childCategories as $ccat){
$html .= '';<-- your code'
}}} $html .='';
return $html;}
for get more childrens only add the new variable and get the categories of her child_of
来源:https://stackoverflow.com/questions/3571417/wordpress-categories-and-subcategories