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.

Using Category Groups For Conditionals

January 30, 2012 5:05pm

Subscribe [1]
  • #1 / Jan 30, 2012 5:05pm

    Dazeone

    6 posts

    I am hoping someone can help me figure out how to use conditionals based on the category and category group an entry belongs to.

    Basically I have a channel called Advertisements and each entry is categorized by the topic (in one group) and by the type of advertisement (in a second group).

    Example:

    Ad Categories: automotive, clothing, communications, etc.
    Ad Types: print, online, television, etc.

    I’m assuming the code would look something like this:

    {exp:channel:entries channel="Advertisements" limit="10" {if segment_2}category_id="{segment_2}"{/if}}
    
    {if category_id == "31"} {!-- which is the id for "television" in ad types --}
    
         Code for displaying videos
    
    {if:else}
    
         Code for displaying images
    
    {/if}
    
    {/exp:channel:entries}

    Obviously my conditional (as written) doesn’t work. I’m not trying to limit the query to video files, but rather display the right html to display videos IF the entry belongs to the “television” category.

  • #2 / Jan 31, 2012 3:30pm

    Kevin Smith

    4784 posts

    Hi Dazeone,

    Categories aren’t really meant to be paired together in this way, and so the Channel Entries tag only expects a single category to define which content to show. (In fact, if you wanted to do this with a single category, you could even use the category_request conditional.)

    Depending on how you want to set your views up, you might consider making the Ad Type it’s own dropdown field for the channel and using Dynamic Parameters to show the particular ad type on a page.

    If you’re just trying to use different markup to display different ad types on the same page, you could go with something similar to the above, but give Ad Types its own dropdown field, trigger the ad category in the URL like so, and then have this code to display (where ad_type is your custom field):

    {exp:channel:entries channel="Advertisements" limit="10"}
    
    {if ad_type == "television"}
    
         Code for displaying videos
    
    {/if}
    {if ad_type != "television"}
    
         Code for displaying images
    
    {/if}
    
    {/exp:channel:entries}
    
    I made the conditionals simple conditionals since it was possible to do so and since simple conditionals are parsed earlier and therefore improve performance. Does this help?
  • #3 / Feb 02, 2012 2:28am

    Dazeone

    6 posts

    Hi Kevin!

    Thanks for the quick reply.

    I had actually gotten it to work using the code below, but after reading your post I am thinking it would make a lot more sense as a field rather than a seperate category. I’m curious to know if using a channel field would be be faster as well?

    {exp:channel:entries channel="Advertisements" limit="5"}
         {categories}
              {if category_name == "Television"}
    
              Code for to display videos
    
              {if:elseif category_name == "Print"}
    
              Code to display images
    
              {/if}
         {/categories}
    {/exp:channel:entries}
  • #4 / Feb 05, 2012 8:31am

    Sean C. Smith

    3818 posts

    Dazeone,

    Glad to see that you’ve got it working now. Your code is fine, but I also like Kevin’s code a better. There should be no difference in speed if it’s a field or a category.

    Is there anything else I can assist you with?

    Sean

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

ExpressionEngine News!

#eecms, #events, #releases