2

Running EE 3.5.15

I am trying to show content based on URL segment and the following is not working correctly:

 {if "{segment_2}" != "portfolio" || "{segment_2}" != "photos"}
  <p>Conditional content.</p>
 {/if}

If I use one of the conditionals by itself it works correctly, but when I introduce the OR into it, it fails.

I have tried the following:

{if "{segment_2}" != "portfolio" OR "{segment_2}" != "photos"}

and

{if segment_2 != "portfolio" || segment_2 != "photos"}

I have also tried it with single quotes.

Any suggestions on this elementary issue will be appreciated.

3 Answers 3

4

Well here is the issue. You are trying to give OR condition for 2 strings. OR condition will return true if any one of the given statements are true.

If you want to execute the code when url_title not equal to "portfolio" neither equal to "photos" use this condition:

{if segment_2 != "portfolio" && segment_2 != "photos"}
    <p>Conditional content.</p>
{/if}

Or as a simle way:

{if ! "portfolio|photos" *= segment_2}
    I am here
{/if}
1
  • 1
    Thanks Mufi. While the logic at first does not make sense because technically you can't be on both pages at once, it does work and who am I to argue with success?
    – forrest
    Commented Feb 16, 2018 at 14:17
3

You should try your code in below manner.

{if ! 'portfolio|photos' *= segment_2}
    <h1>Conditional content.</h1>
{/if}

You can also try in another way also.

{if segment_2 == 'portfolio' OR segment_2 == 'photos'}
{if:else}
    <h1>Conditional content.</h1>
{/if}
1
  • Harsh, thank you for the suggested solutions. The first one did not work, although I wish it had because it is tidy. The second option did work.
    – forrest
    Commented Feb 16, 2018 at 14:15
0

Try

 {if segment_2 != 'portfolio' OR segment_2 != 'photos'}
        <h1>Conditional content.</h1>
 {/if}

I've just tested, it works fine.

1
  • I tried it and it will not work for me. Thank you though.
    – forrest
    Commented Feb 16, 2018 at 14:18

Your Answer

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

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