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.

OR conditional returns true multiple times but needs to output once

July 24, 2009 8:19am

Subscribe [4]
  • #1 / Jul 24, 2009 8:19am

    Jolle

    38 posts

    I want to automatically add a banner on the bottom of ‘News’ entries that have specific categories assigned to them. This all works just fine, but when articles have multiple categories assigned to them that both corresond with the conditional, the banner shows equal amount of times.

    The code below (from within weblog:entries tags) then would show “Showing bannerShowing banner” for entries that would have both category 33 and 37 attached to it.

    {if weblog=="News"}
       {categories}
          {if category_id=="33" || category_id=="37"}
             Showing banner
          {/if}
       {/categories}
    {/if}


    Is there any way to display only once?

  • #2 / Jul 24, 2009 9:06am

    ender

    1644 posts

    could use php:

    <?php $showbanner = false; ?>
    
    {if weblog=="News"}
       {categories}
          {if category_id=="33" || category_id=="37"}
            <?php $showbanner = true; ?>
          {/if}
       {/categories}
    {/if}
    
    <?php if($showbanner) { echo "showing banner"; } ?>
  • #3 / Jul 24, 2009 9:43am

    Jolle

    38 posts

    Brilliant, works like a charm. Thanks so much!

  • #4 / Jul 24, 2009 11:08am

    Boyink!

    5011 posts

    I also wonder if..

    {if weblog=="News"}
       {categories limit="1"}
          {if category_id=="33" || category_id=="37"}
             Showing banner
          {/if}
       {/categories}
    {/if}

    Would work?

  • #5 / Jul 24, 2009 11:15am

    Jolle

    38 posts

    Nice thinking! Unfortunately, just checked and no cigar, multiple outputs with that.

  • #6 / Jul 24, 2009 11:16am

    ender

    1644 posts

    Not sure limit applies to the categories tag, but I would think there’d be a chance that the category it returned was neither 33 or 37 but had it been allowed to run through them all one of those categories would be in the set somewhere, so you’d get false negatives, so to speak.

  • #7 / Jul 24, 2009 11:17am

    silenz

    1651 posts

    If an entry can have multiple categories, then probably not, otherwise yes.

  • #8 / Jul 24, 2009 11:20am

    Boyink!

    5011 posts

    Not sure limit applies to the categories tag,

    Yep...😉

    I wasn’t sure if other categories might be selected.

    But—with that limit in place you shouldn’t get multiple outputs….it might be the wrong one but should only be one..

  • #9 / Jul 24, 2009 11:25am

    ender

    1644 posts

    well assuming it works like the docs show… this might theoretically work:

    {if weblog=="News"}
       {categories limit="1" show="33|37"}
          {if category_id=="33" || category_id=="37"}
             Showing banner
          {/if}
       {/categories}
    {/if}

    it’s possible that EE doesn’t like the || in the conditional… could try using OR instead.  I know it shouldn’t matter, but it has for me in the past.

  • #10 / Jul 24, 2009 11:31am

    Jolle

    38 posts

    Mike, I think you may very well be right after all, just did a little sandbox test on my own site and this did seem to work. This specific example is for a client though, and they’re still running 1.6.0 :sick:

  • #11 / Jul 24, 2009 11:44am

    Ingmar

    29245 posts

    Please urge them to upgrade in the strongest possible terms. 1.6.0 is over 2 years old.

  • #12 / Jul 24, 2009 11:52am

    Jolle

    38 posts

    I know, I was called in a month ago to help out and was shocked. The thing is that the previous guy working on it had hardly any EE experience and used PHP all over, there’s ancient plugins being used all over to perform standard tasks, it’s a complete Frankenstein installation now.
    Because the maintenance has been so poor over the past 1-2 years they’ve decided to build a new website in another CMS. Nothing I can do about that, that course has been set 6 months ago and the new site is already being built. I’m just there to see things through till the new site is ready. Sad, really…

  • #13 / Jul 24, 2009 12:39pm

    Boyink!

    5011 posts

    Sometimes I think a strong knowledge of PHP is the biggest handicap for learning EE.

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

ExpressionEngine News!

#eecms, #events, #releases