In which we get all....related-like. Using categories to display services related to products and vice-versa.
So in Chapter 14, we implemented the Services section of our model site, and assigned those services to the same categories as our existing Products.
In this chapter, we’ll display Services related to Products on the Products category pages, and Products related to Services on the Services category pages. Confused yet?
We’ll need two new embedded templates (one each for related products and related services), and we’ll need to edit the two existing category templates for those sections.
Note that the full-text versions of the templates will be available at the bottom of this post.
New Related Services Template
This template will go on the Products category page, and pull in the Services that are assigned to the same category as the currently-viewed Products category. The template is a simple one, and gets saved into your “embeds” template group:
<h3>Related Services</h3>
<div class="lcontent">
<ul>
{exp:weblog:entries weblog="bees_services" disable="categories|pagination|trackbacks|member_data|custom_fields"}
<li><a href="{url_title_path=building_series/chapter_15_services_detail}">{title} >></a></li>
{/exp:weblog:entries}
</ul>
</div>
This template is just a simple weblog:entries tag, with as much disabled as possible that returns an unordered list of linked titles.
But wait - don’t we need to somehow tell the tag what category we need? There is the category parameter and we could grab the category from the url using segments and.....
Hold on.
I’ve found that just about every time I’ve started down the path of forcing the weblog:entries tag to display a specific category it’s been unnecessary. Why? Because EE is going to do it anyway. The most important thing to note about this template is what isn’t there - and that’s the “dynamic="off” parameter that is usually required for sidebar content to display correctly.
In this case, we want the weblog:entries tag to be dynamic, as then EE will dynamically grab the category from the URL and use it filter the list of services to just the ones that are the same category group as the products being displayed.
New Related Products Template
Got it? Good. Let’s make another one - this one to pull in products that are in the same category group as the Services category being displayed. Save it into your “embeds” template group as well:
<h3>Related Products</h3>
<div class="lcontent">
<ul>
{exp:weblog:entries weblog="bees_products" disable="categories|pagination|trackbacks|member_data|custom_fields"}
<li><a href="{url_title_path=building_series/chapter_15_product_detail}">{title} >></a></li>
{/exp:weblog:entries}
</ul>
</div>
Editing the Products Category Template
Now all we need to is add our new Related Services embedded template into the existing product_list template. Let’s put this new content at the top of the right column so it won’t be missed:
<div id="right_side">
{embed="building_series_ch15_embeds/related_services"}
{embed="building_series_ch15_embeds/latest_news"}
{embed="building_series_ch15_embeds/latest_products"}
</div><!-- close right_side -->
Editing the Services Category Template
And same with the existing services_list template, just embedding the related_products instead:
<div id="right_side">
{embed="building_series_ch15_embeds/related_products"}
{embed="building_series_ch15_embeds/latest_news"}
{embed="building_series_ch15_embeds/latest_products"}
</div><!-- close right_side -->
The Working Example
Ready to see this in action? OK - let’s look at two sides of the same category - Accessories. First, here is the Products List, showing our Accessories products in the main column, and related Accessories Services in the right column. Now let’s flip to the other side - here is the Services List - showing the Services in the main column and the related Accessories Products in the sidebar.
I’ve seen a few posts in the ExpressionEngine forums wondering what Categories were really useful for - and I hope this shows one solid example. Being able to group and relate content from different areas of your main navigation is often very handy to do - essentially letting you slice your site horizontally as well as vertically. Keeping in mind that each weblog can have multiple category groups assigned to it and the possibilities grow even more.
Templates
Here are the full-text versions of the templates used in this chapter. Note again that I have chapter-specific groups for my embeds, so you will likely need to edit those paths to match your setup.
Next up - beginning the Weblog implementation.
Back to Chapter 14.
On to Chapter 16.
Visit Train-ee.com for the latest in ExpressionEngine training designed with one goal in mind - to get you up to speed on ExpressionEngine® as quickly as possible.
The latest Train-ee Products:
The latest from the Train-ee blog:
December 07, 2007
Thanks Michael, your pro building series came through, at just the right time, when http://www.jambor-ee.com skipped a beat!
I saw where you were following along on those too.
I’ve been dreaming up a design community website, categories will come in handy there, associating a site of the day with color, layout, code, dev EE jquery.
My day job shot down my hopes of running php/EE again :(
I’m going to pitch zend core w/fastCgi for iis to them soon, and keep my fingers crossed.
December 11, 2007
Mike—Thanks for Chapter 15! I’ve been wondering about dynamic="off" all along, and this is a great example of when we should leave dynamic="off"...uh....off!
December 12, 2007
These tutorials for EE are great. This series and your articles on EE have all been easy to follow and make sense to someone who is new to EE. Thanks again Michael.
December 23, 2007
Hey man Awesome Tutorial, I am all done, except I need chapter 16. I need to add a blog and I’m good.
Also I wanted to ask if you could include how to show most 5 recent blog entries on the homepage.
Thanks
December 26, 2007
I saw you got help over on Godbit…
I’m working on Chapter 16, but the last couple of weeks have been a bit crazy even without the holiday. This is why I didn’t commit to a publishing schedule...
January 15, 2008
FYI: The Building an ExpressionEngine Site series is now available for purchase as an eBook on the newly-launched Train-ee.com.