Список отображаемых категорий и подкатегорий поста в WordPress

Это кажется очень простым, но я не знаю, почему приведенный ниже код не работает. Я искал все в Google, есть много решений, но не работает для меня. Ребята, пожалуйста, дайте мне знать, что мне не хватает.

Мой код ниже:

    <ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0' //get top level categories only
);

$all_categories = get_categories( $get_parent_cats );//get parent categories

foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;

echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
$get_children_cats = array(
'child_of' => $catID //get children of this parent using the catID variable from earlier
);

$categories = get_categories($args);

$child_cats = get_categories( $get_children_cats );//get children of parent category
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;

//for each child category, give us the link and name
echo '<a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';

}
echo '</ul></li>';
} //end of categories logic ?>
</ul><!--end of category-sidebar-->

Это только дает мне категории, но не подкатегории в них.
Пожалуйста, помогите кому-нибудь.

Заранее спасибо.

1

Решение

Эй, я нашел решение:

<ul class="category-sidebar">
<?php
$get_parent_cats = array(
'parent' => '0','hide_empty' => false //get top level categories only
);

$all_categories = get_categories( $get_parent_cats );//get parent categories

foreach( $all_categories as $single_category ){
//for each category, get the ID
$catID = $single_category->cat_ID;

echo '<li><a href=" ' . get_category_link( $catID ) . ' ">' . $single_category->name . '</a>'; //category name & link
$get_children_cats = array(
'child_of' => $catID,'hide_empty' => false //get children of this parent using the catID variable from earlier
);

$categories = get_categories($args);

$child_cats = get_categories( $get_children_cats );//get children of parent category
echo '<ul class="children">';
foreach( $child_cats as $child_cat ){
//for each child category, get the ID
$childID = $child_cat->cat_ID;

//for each child category, give us the link and name
echo '<a href=" ' . get_category_link( $childID ) . ' ">' . $child_cat->name . '</a>';

}
echo '</ul></li>';
} //end of categories logic ?>
</ul>

Спасибо всем за ваше время. 🙂

0

Другие решения

Других решений пока нет …