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.

Query regarding categories

August 28, 2010 11:22am

Subscribe [3]
  • #1 / Aug 28, 2010 11:22am

    mathalete

    86 posts

    Hi there,

    This is probably me being a bit thick (wouldn’t be the 1st time), but I need to test the logic of what I’m trying to do.

    I just write a simple condition that will do the following >

    1. Open a weblog
    2. If the category id is = x then do then do this
    3. if:else the category id = y, then do that.

    The examples that i’ve seen on this site always call the category id in the weblog tag

    eg:

    {exp:weblog:entries weblog="news" dynamic="off" limit="3" orderby="date" category="22"}

    What I want to do is run a conditional to do stuff based on what the category id is.

    I’ve included some quasi-code here for reference…

    {exp:weblog:entries weblog="news" dynamic="off" limit="3" orderby="date"}
    {if category="22"}
       foo
    {else:if category="23"}
       bar
    {/if}

    I can’t seem to get this to work, and it’s probably either due to me not fully getting my head around categories, or my syntax is wrong.

    Cheers

  • #2 / Aug 30, 2010 3:27am

    John Henry Donovan

    12339 posts

    mathalete,

    Try it with the following changes. Use the categories tag pair when referencing to category variable inside the weblog:entries tag, Use category_id and not just category. It should be if:else and not else:if

    {exp:weblog:entries weblog="news" dynamic="off" limit="3" orderby="date"}
    {categories}
    {if category_id="22"}
       foo
    {if:else category_id="23"}
       bar
    {/if}  
    {/categories}
  • #3 / Aug 30, 2010 7:58am

    mathalete

    86 posts

    Hmm… that didn’t work.

    Do you know if this is something that is outwith the purposes of the basic tags?

  • #4 / Aug 30, 2010 5:58pm

    Ingmar

    29245 posts

    In your code the conditional was incorrect. It’s {if} ... {if:else} ... {/if}. That said,

    {exp:weblog:entries ... category="22"}

    will never show anything but cat_id 22 so I don’t see how the conditional would even be relevant here.

  • #5 / Aug 31, 2010 5:16am

    mathalete

    86 posts

    Yes, I corrected the code as per John’s example, and this unfortunately didn’t work.

    I understand that explicitly calling a category in the weblog:entries tag, will only call entries from said weblog.

    My question still remains, you have one weblog with more than one category assigned to it.

    You want to be able to show entries from said weblog, and separate them by category.

    So, can a conditional do this (or is my syntax just incorrect)? Is there a better way of acheiving this? Is there a plugin that can assist?

    How about

    {exp:weblog:entries weblog="news" dynamic="off" limit="3" orderby="date" category="22|23"} 
    {categories}
    {if category_id="22"}
       foo
    {if:else category_id="23"}
       bar
    {/if}  
    {/categories}
  • #6 / Aug 31, 2010 5:50am

    Ingmar

    29245 posts

    I am afraid your syntax is slightly off again. If you want to use a second “if”, you’d use it like so:

    {if category_id="22"}
       foo
    {if:elseif category_id="23"}
       bar
    {/if}

    you used if:else instead of if:elseif; if:else would not take any further parameters but rather match any remaining unmatched conditions. This would work as well:

    {if category_id="22"}
       foo
    {if:else}
       bar
    {/if}

    but it would show “bar” in all cases where category_id was not equal to 22 (including 23, of course, but not limited to it). Makes sense?

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

ExpressionEngine News!

#eecms, #events, #releases