Output category titles in parents & children order
-
I am wondering how to output list of children category titles under their parents category title.
For example if Women is a parent category name and there are 3 child categories “skirt, pants and shirts” I want them to be printed
Women- Skirt
- Pants
- Shirts
I currently have following code
<ul> <?php $categories = get_categories('exclude='); foreach ($categories as $cat) { echo '<li><a href="' . get_category_link($cat->cat_ID) . '>' . $cat->cat_name . '<small> (' . $cat->count . ')</small></a>'; echo '<ul class="list1">'; $loop = new WP_Query(array( 'posts_per_page' => -1, 'cat' => $cat->cat_ID )); while ($loop->have_posts()) { $loop->the_post(); $category = get_the_category(); // Only display a post link once, even if it's in multiple categories if ($category[0]->cat_ID == $cat->cat_ID) { echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; } } echo "</ul>"; echo "</li>"; wp_reset_postdata(); } ?> </ul>
But this code doesn’t display “parent and child” structure. Moreover they are all mixed up where I cannot tell which one is parents and their children.
I would like to do the parents and children structure applies to this post category.
Your help is very much appreciated!
Thank you!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Output category titles in parents & children order’ is closed to new replies.