Custom Loop through category menu to include sub categories

In my custom wordpress theme I currently have the below loop to the main categories

<?php
$menu = wp_get_nav_menu_object('navigation');
$items = wp_get_nav_menu_items($menu->term_id);
foreach($items as $item)
{
?>
    <li><a href="https://wordpress.stackexchange.com/questions/214199/<?php echo $item->url ?>"><?php echo $item->title ?></a></li>
<?php
}
?> 

So it’ll just list out each like so:

  • Cat A
  • Cat B
  • Cat C
  • Cat D

But I have now added some subcategories under Cat B so I would like to have it as so:

  • Cat A
  • Cat B
    • Cat B 1
    • Cat B 2
  • Cat C
  • Cat D

I know what to do regarding the html markup but what php command do I use in the loop to distinguish the subcategories?

0

Leave a Comment