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.

How to conditionally filter entries with multiple categories assigned.

November 03, 2010 11:23am

Subscribe [3]
  • #1 / Nov 03, 2010 11:23am

    BCPower

    8 posts

    {exp:channel:entries channel="wods" year="{segment_3}" category="{segment_6}" month="{segment_4}" sort="desc"} 
    
    <div class="blogroll-post">
      <h1><a href="http://{site_url}index.php/mojo/wod-entry/{entry_id}">{title}</a></h1>
      <h3>Posted for 
          {categories}
            {if category_id == '9' AND category_id != '11'}Boot Campers
            {if:elseif category_id == '11'  AND category_id != '9'}Veterans
            {if:elseif category_id == '9' AND category_id == '11'}Everyone
            {/if}
          {/categories} 
    on {entry_date format="%F %j, %Y"} by {author} |</h3>  <h4><a href="http://{site_url}index.php/mojo/wod-entry/{entry_id}">{comment_total} Comments</a></h4>
    <h2>{wod_body}</h2>
    
    <p><!-- end .blogroll-post --></div></p>
    
    <p>{/exp:channel:entries}

    The above code will output “Boot CampersVeterans” because some entries are assigned both categories.  However, I would like to output “Everyone” if both categories are assigned.  How can this be accomplished?

  • #2 / Nov 03, 2010 6:04pm

    Ingmar

    29245 posts

    You could reverse the order of your conditionals:

    {if category_id == '9' AND category_id == '11'}Everyone
    {if:elseif category_id == '11'  AND category_id != '9'}Veterans ...

    That said, category_id can’t be “9” and “11” at the same time, so that won’t work as such.

  • #3 / Nov 04, 2010 11:43am

    BCPower

    8 posts

    Why not?  I can assign two categories.

  • #4 / Nov 04, 2010 12:44pm

    BCPower

    8 posts

    Although I haven’t figured this out exactly as asked, my workaround was to simply create another category called Both and use its category_id in my conditionals.

  • #5 / Nov 05, 2010 3:59am

    John Henry Donovan

    12339 posts

    BCPower,

    While you can certainly assign more than one category per entries, evaluating them is a bit more difficult than that. Inside that categories tag pair it loops through the categories and returns only one each time so it is impossible to evaluate both in a conditional at the same time. It will miss out on the second half of your conditional each time.

    Your workaround is a good one though.

    This thread is more suitable for the CodeShare Corner so moving there

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

ExpressionEngine News!

#eecms, #events, #releases