Hey wonderful programmer peoples!
I have a fully functioning PHP snippet which lists categories and their posts, but we now have subcategories! I'd like to extend the current script but at a loss where to start, my PHP knowledge is quite basic but I'm learning..
The function of the script below is to:
- List categories.
List link to post within the category
<div class="menu-main-menu-container"> <ul id="primary-menu" class="menu nav-menu" aria-expanded="false"> <?php $cat_args = array( 'taxonomy' => 'products-category', ); $categories = get_categories($cat_args); foreach ($categories as $category) { $cat_query = null; $args = array( 'order' => 'ASC', 'orderby' => 'title', 'posts_per_page' => -1, 'post_type' => 'products', 'taxonomy' => 'products-category', 'term' => $category->slug ); $cat_query = new WP_Query($args); ?> <?php if ($cat_query->have_posts()) { echo "<li class='page_item'>" . $category->name; echo "<ul class='custom-submenu'>"; while ($cat_query->have_posts()) { $cat_query->the_post(); ?> <li> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </li> <?php } echo "</ul></li>"; } wp_reset_postdata(); } ?> </ul> </div>
What I'd like to achieve is:
- List categories.
- If category has subcategory, list subcategories.
- List link to post within the category/subcategory.
FYI: The above is used as a menu, it currently prints as shown below:

For example, I'd like to change the first menu item 'Bars' to include 'front bars' and 'back bars' (both subcategories) and then list their posts in further submenus.
Thank you in advance for your assistance and knowledge, I hope I have provided enough information! :)


Baris the parent category andfront barsandback barsare subcategory? if yes means, where you want to show the post which belongs to the categoryBars- Ramya