1

I'm attempting to get a conditional statement working within category id parameters.

Ideally, I need the title of certain entries to link to details pages but ONLY if they are in category_id 38. Then, I'd like to show just the title (without links) only when entries are not in category_id 38.

This is what I've come up with so far. I have never attempted something exactly like this before. I am getting a parse/syntax error. Could someone take a look and help me to see what I am doing wrong, please and thank you!!

{categories}

{if category id == "38"}

    <h4><a href="{url_title_path='product-support/service-centers'}">{facility_title}</a></h4>

{if:else category_id == "32|30|33|31" && category_id != "38"}

    <h4>{facility_title}</h4>

{/if}

{/categories}
1
  • what happens if an entry has two categories? ex: category 30 and 38, then the category_id == "38" will fail b/c category_id = "3038"
    – lelkins001
    Mar 2, 2015 at 21:03

3 Answers 3

1

The categories variable pair performs a loop. Always. If your entries has more than one category, it will show all of them. So, if you want to show just one, you have to limit it.

Remember that ==, !=, >, < or any other comparison operator is available just on if or elseif. else haven't operators. It executes on all possible exceptions to the previous conditions.

If you want to compare against several values at once, you gonna need something like an IN operator, which isn't currently available on EECMS template language. But, it looks like, for your question, that you just cares about the category 38 to have a link. So, you don't need to test for the others.

{categories limit="1"}
    <h4>
        {if category_id == "38"}
            <a href="{url_title_path='product-support/service-centers'}">{facility_title}</a>
        {if:else}
            {facility_title}
        {/if}
    </h4>
{/categories}
1
  • AWESOME, that did it. Thank you so much! :) Jun 10, 2014 at 15:17
0

Try:

{categories}
    {if category_id == "38"}
        <h4><a href="{url_title_path='product-support/service-centers'}">{facility_title}</a></h4>

    {if:elseif category_id == "32" OR category_id == "30" OR category_id == "31" OR category_id == "33"}
        <h4>{facility_title}</h4>
    {/if}
{/categories}

1) missing underscore in category id 2) elseif instead of else 3) no need to check for category 38 again docs on conditionals

5
  • Thank you for the reply. This is getting rid of my error, but I'm getting two H4s. Since entries can be in both 38 and other categories, it's showing two headlines. I tried adding && category_id != "38" but it's still showing both headlines. Jun 9, 2014 at 18:06
  • For testing, add {category_id} into your h4 tags to see what category id is outputting each tag. Jun 9, 2014 at 18:24
  • Ok, I added {category_id} right before both H4 tags and the first line says 38 and the second says 32. The output looks like this: <h4>38West Palm Beach</h4> <h4>32West Palm Beach</h4> There is only one west palm beach entry, it's just in both of those categories. I can't figure out why would this output two H4 tags? Jun 9, 2014 at 20:24
  • The reason it's outputting 2x is because the {categories} tag pair loops, and if there are multiple categories for that entry it will loop multiple times. So you end up with a match for 38 in one loop and a match for 32 as it loops the second time. There's not a quick way that I know of to do what you're asking, but I would approach this using Stash add-on (and will try to add an example later, or someone might have another solution first). Jun 10, 2014 at 13:58
  • Thank you so much for your diligent help! I got some additional help and I was able to fix the looping problem with a limit=1 on the categories tag. :) Thanks!! Jun 10, 2014 at 15:18
0

You're along the right lines.

Let's break down your code section-by-section

{categories}
{/categories}

This will work just fine.

 {if category id == "38"}

The correct variable name here is {category_id} (note the underscore). Getting the variable names correct is imperative when using conditionals - because else you're checking against the wrong thing.

 <h4><a href="{url_title_path='product-support/service-centers'}">{facility_title}</a></h4>

No problems here

 {if:else category_id == "32|30|33|31" && category_id != "38"}

This is where it starts to break down. What you have effectively written is "if it's anything but 38 do this instead". But you've hard-coded that. What if category 34 appears tomorrow? You'd be better off going for a simple {if:else} line. That does the same thing short-hand.

The second issue is that you use {if:else} here, but you wrote it as a {if:elseif} statement. Notice the second if there. That allows you to basically say "If the above conditional isn't true, try this new conditional instead".

Additionally you're using piped values (|) to check the category_id variable. This is the syntax for selecting multiple entries but it wouldn't be outputted like that. Unfortunately in conditionals you'd have to go long hand with {if:elseif category_id=="32" || category_id=="30" || [etc]} - that's no fun. For your reference though, doing a double-pipe (||) in conditionals is to OR what && is to AND.

And so if we put it all back together, this code will do the job.

{categories}
    {if category_id == "38"}
        <h4><a href="{url_title_path='product-support/service-centers'}">{facility_title}</a></h4>
    {if:else}
        <h4>{facility_title}</h4>
    {/if}
{/categories}
1
  • Thanks SO much for your help with this. :) I have it working now, I am very grateful for your detailed advice!! Jun 10, 2014 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.