Categories : Super clean URL's and optional sub categories

ExpressionEngine has had categories built in since the carnation. The issue with them is that have the keyword “category” ( which can be changed) in the url. This is really not a major problem but at Media Surgery we like to keep our urls clean and tidy and that just make sense to users. Some will say that its big SEO issue which its not, but thats a discussion for another rainy day. Taking out the keyword is pretty straight forward but we built a site which not only had categories but sub categories, again, not a massive deal but we wanted to make it robust and there was a small caveat. Some categories had a sub category and some had not. Some may ask why would you go to writing a lot of code for something relatively simple and couldn't you just use the fantastic Structure module? Well yes but Structure isn't for all projects and sometimes we like to challenge ourselves to build things in different ways. Making this robust as possible, as with any project, is something very important to us. Using categories in this also manner makes it incredibly easy for the client to update and will allow the site to grow with the client being in full control. With limited resources as with all small web design agencies you don't want clients phoning or emailing you after launch with questions and queries, writing code like this just makes good business sense.

I will explain how to build dynamic categories and super clean urls. Just the way we like it!

This tutorial expects a good understanding of ExpressionEngine and is not for beginners.

If you are starting out with ExpressionEngine we highly recommend the fantastic Mijingo Screencasts ( well worth the price tag) and Train-ee training series.

We are using EE version 2 but should work in EE1 with little mods.

In planning this project its a good idea to get some pen and paper out and write the urls you want to create. Its all very well having it inside your mind but writing it all down will reinforce your understanding of how its all going to go together. We are using segments 1-5 but the code can easily be rewritten for a simple site for segment 1-3 or could even go deeper with many more subcategories. This code is an example and its highly recommend you experiment with it to fit your own needs. There is more than one way to skin a cat so if you have a better method please comment. Some may spot improvements in the code so please post and we can all learn from each other.

  1. 1.)Install Child Categories from Devot-ee ($20 but a good investment)
  2. 2.)Install Low Seg2Cat ( free thanks to the awesome Low)
  3. 3.)Setup simple weblog with a summary and a body field.
  4. 4.)Set up categories with sub categories and assign to weblog.
  5. 5.)Populate with entries in that weblog, we are using animals and fish as examples and of course it goes without saying, you can use any information. Its the structure thats important here not the content.
  6. 6.)Code section 1: We listing the categories and only parents using parent_only=”yes”
  7. 7.)Code section 2: This is the most complex part of the template but as with most code its straight forward when you break it down. We are using the query module to get the count of the number of sub categories in that category. Its using low-seg2cat to pull the category from the segment3. If the subcategories is 1 or more then the sub categories are going to show. This is where the child categories plugin is used to display those subcategories. If there are no sub categories then the list of animals/fish using standard channel ( weblog if EE1) entries tag. Again we are using Low Seg to Cat for the value and the categories parameter to take in the correct data to the channel loop.
  8. 8.)Code section 3: Looking at this code first off you probably are thing what are all these extraneous conditional statements doing here? Unfortunately they are required as the seg to cat plugin doesn't accept advanced conditionals. Low has tried to keep the plugin with as little overhead and as lightweight as possible and rightly so, after all its only a plugin. In laymens terms this section of the code is checking whether a category exists in segment 4. If one exists then display links to the entries. If one doesn't exist then this must be a full listing page so then use the standard channel entries tag to display full information about that animal or fish.
  9. 9.)Code section 4: This is the final and easiest part of the code to understand. The initial conditional is simple asking whether segment 5 exists. If it does, then due to the url structure and set up this is an entry page which is in a sub category. Therefore display the full information from that entry.

Again this isn't a beginner tutorial but with breaking the example down and experimenting with the code and plugins this should be straight forward enough to understand to people with a good grasp of ExpressionEngine.

Thanks for watching and if you have any comments whatsoever please reply below.