Wordpress Categories and SubCategories

元气小坏坏 提交于 2020-01-06 18:10:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!