Display children of the category a post is assigned to

Question

The following code works to display all child categories of a parent (when viewing a parent category), and all siblings of a child category (when viewing a child category) within sidebar.php. The same sidebar is pulled into both posts and pages.

I’d like to take it a step further so it also works when viewing a post, instead of only categories and child categories. On a post, I’d like all child categories of the category assigned to a post to display. After spending countless hours, I’m at a loss. Can anyone help me with this?

    <ul>                
            <?php if (is_category( )) {                             
                  $cat = get_query_var('cat');
                  $thiscat = get_category ($cat);
                  $parent = $thiscat->parent;
                  $img = get_option('z_taxonomy_image' . $thiscat->term_id);

                  if ($parent != '') {

                        wp_list_categories( array(
                        'child_of' => $parent,
                        'exclude' => $cat,
                        'title_li' => 0
                    ) );
                    }

                  else {

                        wp_list_categories( array(
                        'child_of' => $cat,
                        'title_li' => 0
                    ) );
                  }

            } 
            ?>

            </ul>
0
jlohse22 5 years 2016-12-22T17:17:34-05:00 0 Answers 70 views 0

Leave an answer

Browse
Browse