EE: Display multiple categories

I recently started a project based on ExpressionEngine - however there were two requirements: The first one was to display multiple categories - meaning that I wanted a page to display entries that belong to category x as well as to category y. The second requirement was to change categories dynamically.

Requirement 1

The first requirement - displaying multiple categories - is quite easy to achieve. Remember that we can add a “category” attribute to any {exp:weblog:entries} tag to control which categories should be displayed. Use category="2" to just display all weblog entries associated with category 2 - use category="2|4|6" to display entries that are associated with category 2, 4 or 6. You can also exclude category by just placing a “not” in front of the respective id. Simple huh?

Requirement 2

The second requirement is a bit more tricky. Remember, I wanted to dynamically alter the set of categories displayed. In a first step I played with the thought to use the search module - but that didn’t work. It’s easier to dynamically insert the respective values using PHP. I know it’s not recommended to make use of PHP in your templates due to security issues. In my case however I thought it’s acceptable. So here we go:

Solution

Those of you familiar with EE might know that you can display entries of a certain category by adding “C” plus the respective ID to the URL: e.g. …/journal/categories/C2. In this case EE would display all entries filed under category 2.

My approach was to define multiple categories “…/journal/categories/C2&C4&C6”, parse the query string via PHP, replace all “&” with “|”, assign the generated value to the variable “$query” and pass its value to the category attribute:

{exp:weblog:entries weblog="{my_weblog}" category="<?php echo $query;?>" dynamic="off"}.

Hint: Please note the dynamic=”off” attribute that prevents EE from trying to parse the URL. Also make sure that you’ve turned on PHP parsing and that “PHP Parsing Stage” is set to “input”. Otherwise it won’t work. Have fun :)

5 comments so far

Skip to comment form

Dominik Lenk November 10, 2006 at 09:36 PM

Sweet, I have been trying to accomplish something similar for the past couple of weeks… How would you accomplish this using two weblogs though?

Jesse Bennett-Chamberlain November 10, 2006 at 11:15 PM

I think I remember doing something similar, except I just placed the category numbers in the url separated by pipes. So your url would look like ”/journal/categories/2|4|6” and your weblog tag would look like

{exp:weblog:entries weblog=”{my_weblog}” category=”{segment_3}” dynamic=”off”}

monkey56657 November 14, 2006 at 08:22 AM

So is this whole site like on EE….where can i get more info on EE ?

Wolfgang November 14, 2006 at 03:32 PM

Check out the official site for more information on ExpressionEngine :)

Brandon January 08, 2007 at 08:26 AM

Do you mind sharing what PHP code you used to look at the URL. I have been trying to do the very same thing.

Commenting is not available in this weblog entry.