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 category selection issue

April 12, 2012 3:26pm

Subscribe [2]
  • #1 / Apr 12, 2012 3:26pm

    RedSeal

    7 posts

    I have a channel that has entries that can have 0-10 categories assigned to an individual entry. I need to conditionally display one link or another based on whether a specific category is among the selected categories. Basically in pseduo code this would be:

    {if category == "magic-category"}
    DISPLAY THIS LINK
    {if:else}
    DISPLAY THIS OTHER LINK
    {/if}

    The problem is, this doesn’t seem to work with the way you access categories in a EE entry. Since I can only access categories through the {category} entry pair that means it will loop through my conditional for every category that is selected for the entry. I only want that link to show up once. But if the entry had 3 categories selected, 3 links would show up. And if the entry had 0 categories, 0 links would show up.

    So how can I accomplish what I am wanting to do?

  • #2 / Apr 13, 2012 1:17pm

    Shane Eckert

    7174 posts

    Hello RedSeal,

    Thank you for posting your question here on the ExpressionEngine forums.

    I believe what you are trying to work with is the Category URL title.

    To test, please see what output you get with this.

    {categories}{category}{category_url_title}{/categories}


    If it is category_url_title that you are looking for, please try this inside of your channel entries tag.

    {categories}{if category_url_title == "magic-category"}conditional stuff{/if}{/categories}

    Please test this and let me know if it helps.

    Cheers!

  • #3 / Apr 13, 2012 5:58pm

    RedSeal

    7 posts

    Hi Shane-
    Thanks for the response. No, I was actually saying that the categories tag won’t work because it acts as a loop. So if I put your code along with mine maybe it will be more clear.

    {categories}
    {if category_url_title == "magic-category"}
    DISPLAY THIS LINK
    {if:else}
    DISPLAY THIS OTHER LINK
    {/if}
    {/categories}

    I only want either “DISPLAY THIS LINK” or “DISPLAY THIS OTHER LINK” to be displayed a grand total of exactly once. So if I use the categories tag and no categories are selected for an entry neither of the contents would be displayed at all. Conversely, if I had 3 categories selected for an entry 3 sets of the conditional contents would be displayed. I basically need it to be something like:

    {if ANY CATEGORIES FOR THE ENTRY == "magic-category"}
    DISPLAY THIS LINK
    {if:else}
    DISPLAY THIS OTHER LINK
    {/if}

    But I don’t know how to accomplish this.

  • #4 / Apr 18, 2012 1:06pm

    Shane Eckert

    7174 posts

    Hi RedSeal,

    That’s pretty complicated and I am not sure you could do that with the categories loop.

    You might be able to use the Query Module and build a custom bit of SQL to grab what you need.

    I can also move this over to development and programming, they may have some ideas as well.

    Cheers!

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

ExpressionEngine News!

#eecms, #events, #releases