Working with ExpressionEngine Categories

It’s funny how certain topics come up at different times in the ExpressionEngine world. Over the past couple of weeks I’ve noted many forum threads around EE’s categories so it seemed time to write a tutorial around them.

 

No one seems to get confused over setting up category groups, creating categories and assigning entries to them so I won’t cover that here (sales pitch - that process does get covered in step by step detail in my book).

The confusion seems to come from how to create a reusable template to display categorized results. The channel:entries tag has a category parameter that I think misleads people into thinking they somehow need to force-feed the entries tag with the ID of the category they wish to display, and if that’s the case then can you dynamically set that ID or do you have to create different templates with the ID hardcoded?

This isn’t the case - we’re actually looking at a situation where the dynamic property of ExpressionEngine is valuable.  If you haven’t yet, I would suggest a quick view of the dynamic=“off” explained screencast here on Train-ee.  While the parameter values have changed (EE1 was Off/On, EE2 is Yes/No) the functionality is the same. The screencast focuses on single-entry views but the channel:entries tag dynamically responds in a few different situations - reading single entry URLs, pagination URLs, archival URLs and - what we’re interested in today - category URLs.

How to Create a Reusable Category Template
Here’s a quick screencast that shows a simple EE template and how it responds when fed proper category URLs:

Cool screencast here - but you need Flash!

 

How to Create Category Links
Another short screencast shows how to dynamically create category links that feed the template the category URLs that it needs to be reusable:

Cool Screencast here - but you again need Flash!

 

Note: I didn’t code that second link correctly.  It should be:

<a href="{path='cat_demo/index'}">Cat Demo</a

How To Show Which Categories a Single Entry is Assigned To
This question comes up a bit - and the answer is the {categories}{/categories} variable pair for the channel:entries tag.  This is a “looping variable pair” meaning for every one entry in a channel, it will loop once for every category that entry is assigned to.  This is what allows a given entry to be assigned to multiple entries and have each of those category names get displayed for the entry.

In our little demo code it would be used this way:

<h3>Entries</h3>
<
ul>
    
{exp:channel:entries channel="releases"}
        
<li>{title} posted in {categories}{category_name} {/categories}</li>
    
{/exp:channel:entries}
</ul

If you wanted those to be links use the same {path= linking variable I showed in the screencast - again, because you're using it in a category context, it will return category links.

How to Display Category Hierarchy
ExpressionEngine lets you create a hierarchy in your category group, nesting categories and subcategories as many layers deep as you wish. The important thing to note here is that the hierarchy you create is for display only, not for URLs. In other words, the channel:categories tag I used in the screencast will return a nested list showing your hierarchy; but EE will not create URLs that display in a parent_category/sub_category fashion. Using native code, EE works with one category at a time in the URL and they will all display in the same /category/category_url_title fashion.

How To Show Category and Entry URL Titles in the same URL
So I’ve clicked the “salsa” category, got my list of results, and then click on an entry in that list like “Harrison’s Hot Trotters”.  Now I want to see a detail page for that entry that has a URL like: /cat_demo/salsa/harrisons-hot-trotters.  How do I do that?

The short answer is, you don’t.  At least not with native code.  EE just isn’t designed to work with URLs in this way.  Categories were designed to be a simple filter used to whittle down a long list of entries to something more manageable.  Once you chose an entry from that list your URL is going to lose the category information.  One reason for this is that ability to assign an entry into multiple categories - if the category stayed in the URL you could end up with the same single entry showing at multiple URLs.

I don’t believe EE categories were ever designed to be used as a navigation-building tool.  EE has a blogging heritage and its category functionality grew out of that - giving users the ability to filter a large set of blog entries down to just entries in designated categories.

You Keep Mentioning Native Code…
As usual with ExpressionEngine any time the native abilities come up short the 3rd party market rushes to fill that void.  There are a number of category-related add-ons available and in future posts I’ll look at a couple of them that can ease some of your category pain.

Category Navigation

<< Previous Entry   

 

Previous Comments

Picture of nomoretables

by nomoretables

Date: Sunday, February 12th, 2012
Comment: #1

Hey Mike, I know you made the tutorial just for me, since I was the one that was keeping you busy in the forums ;)

I was able to make a lot of progression now that I really understand how the dynamic parameter works, and path= instead vs url_title_path

Now for a tutorial on url segment variables! I’m still having problem with next/previous not cycling in the category the link orginated from. In my next/previous I’m using path= so the next/previous know to watch the url, and my channel pair after that has dynamic=“yes”, but my next/previous still cycles randomly. I’ve tried seg2cat but still no luck.

So I think it’s time for your take on url segments…

Mike Boyink

by Mike Boyink (Author)

Date: Monday, February 13th, 2012
Comment: #2

There were actually about 3 category-related threads that prompted this tutorial..;)

Picture of Brooke Chao

by Brooke Chao

Date: Thursday, March 22nd, 2012
Comment: #3

So, this is exactly the tutorial I needed to find, but unfortunately it’s not working for me!

I have a channel called “portfolio” with several different categories.  I have a template group called portfolio, with index and details templates.  I do have category urls turned on, so say, if my portfolio category is “churches” the url has “portfolio/category/churches” in segments 1,2 &3.

So the links work correctly, and the url renders correctly, and even the category heading renders correctly, but the entries themselves don’t show up, even though they each have one of the categories clicked.

Any ideas why it wouldn’t return any results?  Of course, if I set the dynamic variable to “off” it populates with ALL entries in that channel.

Picture of Boyink

by Boyink

Date: Thursday, March 22nd, 2012
Comment: #4

Hi Brooke - 

You’d have to post the code of your channel entries loop.

Picture of Brooke Chao

by Brooke Chao

Date: Thursday, March 22nd, 2012
Comment: #5

I meant to come back and post earlier. It turns out it was a glitch in the EE CMS.  When I went and unchecked the categories and re-checked them for each entry, suddenly it didn’t have a problem anymore!  So for whatever reason, even thought categories were selected for each, it just wasn’t seeing it.  I think *maybe* it had to do with me changing url/category settings after creating the entries.

Weird, but I’m glad it wasn’t a problem with my code.

Thanks for checking in and being willing to help!

Mike Boyink

by Mike Boyink (Author)

Date: Thursday, March 22nd, 2012
Comment: #6

Cool - glad you got it working.

Picture of Lucas Hibbard

by Lucas Hibbard

Date: Tuesday, April 3rd, 2012
Comment: #7

Hi Mike,

Great article!  I just have one question for you.  Is there an easy way to get all entries that are in NO categories?  Trying to create functionality for an ‘Everything Else’ link that will show all entries that are in no categories.

Thanks, and keep up the great work!

~ Lucas

Picture of Boyink

by Boyink

Date: Tuesday, April 3rd, 2012
Comment: #8

In the back end yes, in the front end no. There was a feature request a while back but at the moment it would take a custom query.

Picture of Davo

by Davo

Date: Wednesday, April 11th, 2012
Comment: #9

Hi there

I couldn’t work out why my code wasn’t working then worked out that I needed ‘Global Channel Preferences > Use Category URL Titles In Links?’ set to ‘Yes’.


Thanks for the tut’s. Life saver.

Picture of Kiril

by Kiril

Date: Tuesday, May 29th, 2012
Comment: #10

Very clear and helpful tutorial, thanks so much for covering the fundamentals. I really appreciate that your still able to keep the mental context of beginners after so many years of EE.

Thx.

Mike Boyink

by Mike Boyink (Author)

Date: Wednesday, May 30th, 2012
Comment: #11

Glad to hear it!

Picture of Mollie

by Mollie

Date: Monday, October 1st, 2012
Comment: #12

This works great, thank you so very much! It listed the categories related to the single post exactly as desired. A small detail issue came up: what is the best way to get the listed categories separated by commas without having a trailing comma at the end? I keep thinking that there is an obvious answer… any ideas?

Picture of Boyink

by Boyink

Date: Monday, October 1st, 2012
Comment: #13

What has your review of the documentation revealed? ;)

Picture of Mollie

by Mollie

Date: Monday, October 1st, 2012
Comment: #14

Er… CSS? li:after and li:last-child:after selectors with comma and non-comma content properties? Is that as spiffy as it gets?

Picture of Boyink

by Boyink

Date: Monday, October 1st, 2012
Comment: #15

Pretty sure the EE docs don’t cover CSS approaches..;)

Picture of trace

by trace

Date: Wednesday, October 10th, 2012
Comment: #16

great tutorial!

@Davo thanks for posting the location of the Global Channel Preference ... i wasnt able to find where it was.

Picture of Rod

by Rod

Date: Friday, October 12th, 2012
Comment: #17

Thanks for the tutorial. Important to note Davo’s comment about setting ‘Global Channel Preferences > Use Category URL Titles In Links?’ set to ‘Yes’. Spent a while before I realised this setting is not default.

Cheers

Picture of Zech

by Zech

Date: Tuesday, October 30th, 2012
Comment: #18

Not working for me, when I add dynamic=“no” it displays all the entries from other categories as well, when I say dynamic=“no” It doesn’t display anything, when I set use category URL titles in links to no It works, what can be the problem?

Picture of Boyink

by Boyink

Date: Tuesday, October 30th, 2012
Comment: #19

So you followed the tutorial using the code provided and it’s not working?

Picture of zech

by zech

Date: Tuesday, October 30th, 2012
Comment: #20

that is my code

Picture of Boyink

by Boyink

Date: Tuesday, October 30th, 2012
Comment: #21

So you used the provided code?  And the provided links?

Picture of zech

by zech

Date: Tuesday, October 30th, 2012
Comment: #22

i don’t have the links because i am working on localhost on my local machine

Picture of Boyink

by Boyink

Date: Tuesday, October 30th, 2012
Comment: #23

I’m not asking for a link to the site. I’m asking if you used the code provided for generating the links. Are you loading the page at a category URL as shown in the screencast?

Picture of Zachariah Z Ngonyani

by Zachariah Z Ngonyani

Date: Tuesday, October 30th, 2012
Comment: #24

okay sorry i thought you were asking for the url, yes i codded a show template that is supposed the category entries the link looks like http://localhost/sitename/faqs/show/category/category-name

Picture of Boyink

by Boyink

Date: Tuesday, October 30th, 2012
Comment: #25

Are you also posting on the EE Forums - maybe this thread:

http://expressionengine.com/forums/viewthread/228783/

?

Picture of pSouper

by pSouper

Date: Wednesday, October 31st, 2012
Comment: #26

hi, I am looping entries and wish to add the list of categories each entry is assigned to but it seems that conditionals break the tag engine.

This works fine…
{categories}{category_name}{/categories}

but this just renders the tags as entered and not parsed
{categories}{if count==0}category:{/if}{category_name}{/categories}

Is this normal and is there a way to use conditionals within the categories tag?

Picture of Boyink

by Boyink

Date: Wednesday, October 31st, 2012
Comment: #27

Since that’s not something covered in this article (and might be a bug) you really need to ask over in the EE support forums.

I don’t mean to be a jerk about it - but if I’m not careful here I end up becoming unpaid EE tech support…;)

Picture of Mantish

by Mantish

Date: Wednesday, October 31st, 2012
Comment: #28

Hi Thanks for the article! It’s very clear

Is there a way to remove the category url indicator?
I’d like to have my url like localhost/cat_demo/xxxx
instead of localhost/cat_demo/category/xxxx

Picture of Boyink

by Boyink

Date: Wednesday, October 31st, 2012
Comment: #29

See above answer..;)

Picture of Mantish

by Mantish

Date: Wednesday, October 31st, 2012
Comment: #30

I found that if I remove the category url indicator, the urls turn into http://localhost/cat_demo/C001

Since I needed “pretty” urls I ended up using {segment_2} and this plugin: https://github.com/mattlanham/Swanify-Category

Anyway, thanks again for the tutorial

Add Your Comment

Commenting is not available in this channel entry.

Unless otherwise stated all content is © Michael Boyink of Train-ee.com & Boyink Interactive. Please don't steal - I've got kids to feed...

dy>