0

I have a series of entries in a channel called "Messages" that need to appear in different ways according to if ALL categories have been assigned, or if less than all have been assigned.

The most obvious (to me) does not seem to be working:

{exp:channel:entries channel="message" status="flex" orderby="date"  sort="desc"}
  {exp:channel:categories category_group="2"}
    {if category_id ==  (12 && 13 && 14 && 15)}
      All the categories have been checked for this entry
    {if:else}
      Otherwise
    {/if}
  {/exp:channel:categories}
{/exp:channel:entries}

I have also tried using the {exp:channel:entries} tag pair:

{exp:channel:entries channel="message" category="12&13&14&15" orderby="date"  sort="desc"}

This channel:entries approach works, but I don't know how to make the query say: "List the entries that do not have ALL FOUR categories checked". (In other words, if you simply do a second loop that filters "12||13||14||15" it will also catch the entries that have all four categories checked.

What is the best way to do this:

  1. Show any entry with categories 12,13,14 and 15 ALL checked.
  2. And then further down the page, show any entry that has ANY of, but NOT ALL of the categories 12,13, and 15 checked.

?

2 Answers 2

0

This was it. Perhaps it could be tweaked for efficiency:

{exp:channel:entries channel="message" status="flex" orderby="date"  sort="desc"}
    {if '{categories show_group="2" show="12|13|14|15" backspace="1"}{category_id}|{/categories}' == "15|14|13|12"}
      All Checked
    {if:else}
     Otherwise
    {/if}
{/exp:channel:entries}
0

This still doesn't seem a great approach, but can't think of a better one, so to get your code working...

It's not recommended to include a channel categories inside a channel entries (or vice versa) due to performance issues, and you don't actually need to, as the information is already included in the channel:entries categories tag.

{exp:channel:entries channel="message" status="flex" orderby="date"  sort="desc"}
    {if '{categories show_group="2" show="12|13|14|15" backspace="1"}{category_id}|{/categories}' == '12|13|14|15'}
      All the categories have been checked for this entry
    {if:else}
      Otherwise
    {/if}
{/exp:channel:entries}

So in the above, this code:

{categories show_group="2" show="12|13|14|15" backspace="1"}{category_id}|{/categories}

Is looping through all the categories assigned to the entry - you don't need to specify the group, but I've added it to show syntax. Specifically we've asked it to bring back the 4 specified categories, then for each loop, output the category ID. We add the backspace to remove the trailing pipe and compare the output. We could just output "X", then match to "XXXX" - same result:

{if '{categories show="12|13|14|15"}X{/categories}' == 'XXXX'}

If you wanted the conditional elsewhere in the template, and not as an else, you can then change the conditional matching:

{if '{categories show="12|13|14|15"}X{/categories}' != 'XXXX'}
3
  • Close, but a couple of errors. However, this was a good idea and I cleaned it up a git. This works: {exp:channel:entries channel="message" status="flex" orderby="date" sort="desc"} {if '{categories show_group="2" show="12|13|14|15" backspace="1"}{category_id}|{/categories}' == "15|14|13|12"} All Checked {if:else} Otherwise {/if} {/exp:channel:entries}
    – M Purdy
    Apr 14, 2016 at 18:36
  • Really? It reversed the category output. Weird. Glad you got it sorted. Apr 14, 2016 at 18:42
  • Yes. And as you said... weird. I figured it out by having it print it. But i think it's a good enough approach for this. We'll be load testing it.
    – M Purdy
    Apr 15, 2016 at 20:09

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.