We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Why does "OR" conditional return two results when an Entry has two categories?

How Do I?

liberty79's avatar
liberty79
135 posts
6 years ago
liberty79's avatar liberty79

I hope this snippet of code is clear enough to serve as a question. I expect the results to be Result A, but instead I get Result B:

Entry has both Categories 1 and 2 assigned to it:

{categories}
 {if category_id == "1" OR category_id == "2"}
  <h1>Category 1 or Category 2</h1>
 {/if}
{/categories}

A: Expected Result

<h1>Category 1 or Category 2</h1>

B: Actual Result

<h1>Category 1 or Category 2</h1>
<h1>Category 1 or Category 2</h1>
       
Robin Sowell's avatar
Robin Sowell
13,158 posts
6 years ago
Robin Sowell's avatar Robin Sowell

Think of the {categories} tag pair as a loop. So- I have 1 entry, it’s in 3 categories, (category 1 and category 2 and category 3).

I output the entry data and when I get to the categories tag pair, the first time through the loop, the category is 1, thus the conditional is true. The second time through the loop, the id is 2, the conditional is also true. The third time through the loop, the category id is 3, so it’s not true and there is no third display.

Depending on what you need to do, you might could achieve it with the show parameter.

       
liberty79's avatar
liberty79
135 posts
6 years ago
liberty79's avatar liberty79

Thanks Robin. Does that tag actually generate a PHP while or for loop?

       
Robin Sowell's avatar
Robin Sowell
13,158 posts
6 years ago
Robin Sowell's avatar Robin Sowell

Hrm, I’m not entirely sure what you’re asking for. In the template, no. Though with php parsed on output, you could treat it as such. That said, we don’t recommend using PHP in templates and it would likely be a case where custom coding a plugin would be a better option.

If you mean in the actual EE php code where it parses the variables and replaces the EE variables with the entry values, I believe it’s a foreach. All of the category data for the entry goes into an array and then replaced. If you want to see ‘the guts’, it’s mostly in mod.channel.php.

And if I totally misunderstood, just let me know.

       
liberty79's avatar
liberty79
135 posts
6 years ago
liberty79's avatar liberty79

Yeah, I was just wondering what’s happening behind the scenes. Thanks.

Anyways, by trying to accurately describe my problem, I figured out I was going about this the wrong way, so…thank you!

       
Robin Sowell's avatar
Robin Sowell
13,158 posts
6 years ago
Robin Sowell's avatar Robin Sowell

😉 I often figure stuff out by writing up my question! Glad you got it figured out on your own!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.