list all child categories that apply to current post even if those cats are empty? everything I tried just shows "post not found" -

Have alphabetical listing and trying to create

A B C D hyperlinked listing

A - All categories that child of A B - All categories that child of B etc





echo '<ul>';
foreach(get_the_category() as $cat) { 
    echo '<li><a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a>';
    $sub_cats = get_categories('parent='.$cat->term_id.'&hide_empty=0');
    if($sub_cats) {
        echo '<ul>';
        foreach($sub_cats as $sub_cat) {
        echo '<li><a href="'.get_category_link($sub_cat->term_id).'">'.$sub_cat->name.'</a></li>';
        echo '</ul>';
    echo '</li>';
echo '</ul>';

edit: for a list of sub categories in a category archive, try this code:

$cat = get_query_var('cat');
$cat_list = wp_list_categories('child_of='.$cat.'&hide_empty=0&title_li=&echo=0');
    if($cat_list) {
    echo 'Sub-category List <ul>';
    echo $cat_list;
    echo '</ul>';


| improve this answer | |
  • this works but only if the category isnt empty - I was looking for a solution that would still pull child categories even if the current category is empty – aj martin Dec 22 '11 at 15:17
  • 'list all child categories that apply to current post ' - can you explain, how can a current category of a post be empty? the suggested code will show empty sub categories. – Michael Dec 22 '11 at 15:59
  • apologies this is my mistake - essentially I need to create a category nav that shows the child and grandchild categories of the current category, whether the current category has posts assigned or not – aj martin Dec 22 '11 at 16:26
  • edited the answer to include some code for the category archive template. – Michael Dec 23 '11 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.