EllisLab text mark
Advanced Search
     
Category-based content
Posted: 04 April 2011 02:02 PM
Joined: 2011-03-04
56 posts

I now have one product channel with 5 categories (books, dvds, audiobooks, posters, and subscriptions). I am currently using the same “product” template to display each product entry in all of the categories.

I want to embed a smaller template into the “product” template for each of the categories.

This means that if a customer is viewing a book in the “product” template, the “details_books” template will be embedded under the images. Likewise, if a customer is viewing a poster in the same “product” template, the “details_posters” template will be embedded instead.

Would this need a conditional? How would I make the conditional choose the template based on which category the product entry is in?

Thank you for your help.

~Sarah

 
Posted: 05 April 2011 04:40 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-10-17
71 posts

Hi Sarah,

You will definitely need a conditional or some sort of switch/trigger to embed the different templates, but there are a bunch of different ways this could be achieved.

Are you displaying the products “by category”—i.e. using a Category segment in the URL? Also, do your Products have multiple categories assigned, or does each Product only belong to one category?

If you’re using a Category segment in the URL, you can just switch on the segment value, like this:

{if segment_3 == 'books'}
    {embed
="products/_books"}
{if
:elseif segment_3 == 'dvds'}
    {embed
="products/_dvds"}

...
{/if} 

You could also look at the Switchee plugin, and/or the Seg2Cat plugin.

HTH

 
Posted: 05 April 2011 06:31 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2004-03-22
12308 posts

Sarah,

Brett’s advice is sound. Does it help?

 Signature 

John Henry’s Website | Follow me on Twitter

 
Posted: 05 April 2011 08:59 AM   [ # 3 ]   [ Rating: 0 ]
Joined: 2011-03-04
56 posts

Brett,

Each product belongs to only one category. I was unable to figure out how to display the products by their category in the URL. So, when a product is viewed in its template, the URL looks like http://www.example.com/products/product/bookname.

Is there a way to add the category name to the URL, or how could the conditional be formed without the category name?

Your advice is greatly appreciated!

~Sarah

 
Posted: 05 April 2011 09:25 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-10-17
71 posts

Hi Sarah,

Have a look at the documentation for the Channel Categories tag. That might help with constructing Category-based navigation. In your case, you’re probably looking to create URLs that look something like this:

http://www.example.com/products/category/books
or
http://www.example.com/products/C12

EE will automatically pick up the categories and filter the output of your Channel Entries tag accordingly.

You might also want to look at this plugin http://devot-ee.com/add-ons/low-seg2cat/.


Alternatively, you could switch your template embeds, based on the Category assigned to each entry using something like this, within your Channel Entries tag.

{if '{categories}{category_url_title}{/categories}' == 'books'}
    {embed
="products/_books"}
{if
:elseif '{categories}{category_url_title}{/categories}' == 'dvds'}
    {embed
="products/_dvds"}

{
/if} 

Note, the above code will only work if you ONLY have one category assigned to each entry (as you do). The {categories} tag pair loops over any/all Categories assigned to an entry (so the above would need to be modified to work if multiple categories are assigned).

It might be worth investigating getting the Category-based navigation working, as this allows EE to do all sorts of stuff *automagically*, which might make your life easier. Otherwise, the above code (or something very much like it) should work for you.

Cheers,

Brett

 
Posted: 05 April 2011 11:27 AM   [ # 5 ]   [ Rating: 0 ]
Joined: 2011-03-04
56 posts

Currently the products are displayed by category on a category page, http://www.example.com/products/category/books/. These category pages display the name of the category with a list of the associated products.

But, when a customer clicks into a single product entry, the category name is no longer displayed. Instead, the URL appears as http://www.example.com/products/product/bookname.

It is the product page itself in which I need to switch the embed based on the category. I have read various tutorials and documentations about channel categories and navigation, but I am still unsure how to either:

1. Add the category to the product page URL, so that I can form a conditional based on the URL.

or

2. Form a conditional without the category_url_title or segment_X ==


These may be silly questions, but I honestly have no idea where to go from here.

Thank you.

~Sarah

 
Posted: 05 April 2011 11:43 AM   [ # 6 ]   [ Rating: 0 ]
Joined: 2007-10-17
71 posts

Hi Sarah,

Give this code a try (same as I posted above) ...

{if '{categories}{category_url_title}{/categories}' == 'books'}
    {embed
="products/_books"}
{if
:elseif '{categories}{category_url_title}{/categories}' == 'dvds'}
    {embed
="products/_dvds"}

{
/if} 

Make sure this is *inside* your {exp:channel:entries} tags. It doesn’t rely on anything in the URL. It uses the {categories} tag pair to look for the Category to which the entry has been assigned.

You can use any of the Category fields for your test, {category_url_title}, {category_name} or {category_id}, whichever works for you.

That should get you started ... it can probably be refined later ...

Cheers,

Brett

 
Posted: 05 April 2011 12:40 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2011-03-04
56 posts

Brett,

Thank you for clarifying the categories tag. I didn’t realize that it wasn’t dependent on the URL.

The code worked, and the product page now displays content based on the category!

The same code worked for the modified price fields (size, color, etc.), which are also different for each product category.

I will definitely be using that code MANY times.

Thank you for sharing!

~Sarah

 
Posted: 06 April 2011 10:43 AM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2002-04-29
26055 posts

Hi, Sarah, glad to see that Brett was able to help.

Brett, thanks for the assist.

Feel free to start a new thread if you have any more questions.