WordPress: Menu items combining multiple categories

On one of the websites I am administrating, posts are assigned two kinds of categories:

  • the age of children
  • for boys or for girls

In the navigation menu, you need to be able to select menu items corresponding to the following types of criteria:

  • Display all posts for boys or all posts for girls
  • Display all posts for children in a specific age category
  • Display all posts for boys in a specific age category or for girls in a specific age category

The first two requirements are easy. You just need to add the corresponding category archives to the menu. For the last one, there is no way to select a combination of categories in the WordPress menu builder.

So you need to add links to the menu and point to the results of a query for a combination of categories. Fortunately, you do not need to write any code to have such a list of post displayed. This can all be done by providing the appropriate parameters in the URL e.g.:

http://mysite.com/?cat=for-boys,6-12-months

for-boys and 6-12-months are the slugs of the corresponding categories.

If you have pretty URLs, it’d look like this:

http://mysite.com/category/for-boys,6-12-months

This will show you posts belonging to either category. What we actually want are posts belonging to both categories. In order to get this result, you need to replace the comma by a plus sign:

http://mysite.com/?cat=for-boys+6-12-months

If you have pretty URLs, it’d look like this:

http://mysite.com/category/for-boys+6-12-months

Instead of “category” you can use “tag” for post tags or “author” for post authors.

One thought on “WordPress: Menu items combining multiple categories

Leave a Reply

Your email address will not be published. Required fields are marked *