ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Category-based content

April 04, 2011 2:02pm

Subscribe [4]
  • #1 / Apr 04, 2011 2:02pm

    FaithBuilders

    42 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

  • #2 / Apr 05, 2011 4:40am

    Brett Gullan

    70 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

  • #3 / Apr 05, 2011 6:31am

    John Henry Donovan's avatar

    John Henry Donovan

    12339 posts

    Sarah,

    Brett’s advice is sound. Does it help?

  • #4 / Apr 05, 2011 8:59am

    FaithBuilders

    42 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

  • #5 / Apr 05, 2011 9:25am

    Brett Gullan

    70 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

  • #6 / Apr 05, 2011 11:27am

    FaithBuilders

    42 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

  • #7 / Apr 05, 2011 11:43am

    Brett Gullan

    70 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

  • #8 / Apr 05, 2011 12:40pm

    FaithBuilders

    42 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

  • #9 / Apr 06, 2011 10:43am

    Sue Crocker's avatar

    Sue Crocker

    26054 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.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases