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.

Conditional Based on Categories

January 10, 2012 7:41pm

Subscribe [3]
  • #1 / Jan 10, 2012 7:41pm

    Ed Hebert

    49 posts

    Hi-

    I’m trying to build a conditional that reacts based on whether an entry is a member of a certain category.

    If I code the following:

    {categories}
    {category_id}
    {/categories}

    I get “1 4 5” (indicating the categories to which this entry belongs.)

    However, if I code this:

    {categories}
        {if category_id=="4"}
          ...do stuff…
        {/if}
    {/categories}

    the {categories} tags don’t parse. They display in the page, and none of my conditional gets executed.

    Can someone please explain the proper syntax for building a conditional which uses a category_id within {exp:channel:entries} tags?

    Thanks

  • #2 / Jan 11, 2012 1:40pm

    Ed Hebert

    49 posts

    Any thought? The version 1.x code from this wiki isn’t working in EE2.

    http://expressionengine.com/wiki/Conditional_Based_On_Category

  • #3 / Jan 11, 2012 3:31pm

    Barrel

    92 posts

    try…

    {categories}
        {if "{category_id}" == "4"}
          ...do stuff…
        {/if}
    {/categories}


    ?

  • #4 / Jan 11, 2012 6:29pm

    Ed Hebert

    49 posts

    Interesting…the code you posted worked, as did the original non-conditional code.

    However, when I put in my “do stuff” code (which is another set of convoluted if/else statements to handle date formatting for an events page), the {categories} tags do not get parsed. They render as text in the template. So looks like the code I’m inserting within the categories tags is breaking something.

    Here’s the full code of what I’m attempting to do:

    {categories}
        {if "{category_id}" == "4"}
            {if expiration_date}
                {if "{entry_date format='%F %j %Y'}" == "{expiration_date format='%F %d %Y'}"}
                    {entry_date format='%F %j, %Y <br /> %g:%i%A'} - {expiration_date format="%g:%i%A"}
                {if:else}
                    {entry_date format='%F %j'} - {expiration_date format='%F %j, %Y'}
                {/if}
            {if:else}
                {if "{entry_date format="%H:%i"}" != "00:00"}
                    {entry_date format='%F %j, %Y   %g:%i%A'}
                {if:else}
                    {entry_date format='%F %j, %Y'}
                {/if}
            {/if}                                                               
        {/if}
    {/categories}
  • #5 / Jan 11, 2012 9:20pm

    Dan Decker

    7338 posts

    Hi Ed,

    I make sure you are consistent in the use of double and single quotes. That could be confusing the parser. Try this:

    {categories}
        {if "{category_id}" == "4"}
            {if expiration_date}
                {if "{entry_date format='%F %j %Y'}" == "{expiration_date format='%F %d %Y'}"}
                    {entry_date format='%F %j, %Y <br /> %g:%i%A'} - {expiration_date format='%g:%i%A'}
                {if:else}
                    {entry_date format='%F %j'} - {expiration_date format='%F %j, %Y'}
                {/if}
            {if:else}
                {if "{entry_date format='%H:%i'}" != "00:00"}
                    {entry_date format='%F %j, %Y   %g:%i%A'}
                {if:else}
                    {entry_date format='%F %j, %Y'}
                {/if}
            {/if}                                                               
        {/if}
    {/categories}

    It may not be evident, but I changed a few doubles to singles 😉

    Cheers!

  • #6 / Jan 11, 2012 10:08pm

    Ed Hebert

    49 posts

    Thanks for the tip, Dan. I do see what you mean about the single and double quotes, and that’s a good reminder for me.

    Unfortunately, no luck! I cut/pasted your code block, and unfortunately got the same error.

    All I see in the rendered template is {categories}{/categories}

  • #7 / Jan 16, 2012 9:28pm

    Dan Decker

    7338 posts

    Ok Ed,

    Thanks for at least entertaining my idea! Let’s get this sorted out. Can you tell me what version of ExpressionEngine 2 you are running and can you post the entire contents of the template that is affected? I’d like to be able to replicate this as accurately as possible, but I want to rule out anything else in the template that might be a factor here.

    Cheers!

  • #8 / Jan 17, 2012 9:53am

    Ed Hebert

    49 posts

    Hi Dan-

    Thanks for your diligence! Unfortunately, after I was unable to find a solution with the categories tag, I reworked the template to present the information differently. So I no longer have the issue with my template, but it’s not because I was able to find a solution to my problem with the {categories} tag.

    Thank you anyway for volunteering your assistance.

    Best,

    Ed

     

  • #9 / Jan 23, 2012 7:32pm

    Sean C. Smith

    3818 posts

    Ed,

    Good to see that you were able to come up with an alternate solution. I’m going to close out this thread, but feel free to post again when you have more questions.

    Sean

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

ExpressionEngine News!

#eecms, #events, #releases