Posted in PHP

WordPress menu from categories, subcategories and posts

Recently we needed to create a custom menu of posts in a hierarchy using the following format:

- category
-- posts
-- subcategory
--- posts
- category
-- subcategory
--- posts

Note that if a category doesn’t have subcategories, the posts are listed in the same level as subcategories.

I discussed the issue with @nielslange and he approached it in the following fashion:

I’ve then modified his code for use in a WordPress menu structure:

This results in the following:

Desktop

Mobile

I’ll add the CSS soon!