3

I am trying to use templates in embeds depending on the users' category selection but I don't know how to check if a category has been assigned or not.

{categories}
    {if category_url_title=="full-page-review"}
        {embed="temp-group/.full_page_review" entry_id="{entry_id}"}
    {if:elseif category_url_title=="standard"}
        {embed="temp-group/.standard" entry_id="{entry_id}"}
    {/if}
{/categories}

This works fine as long as a category has been assigned in the entry, but nothing happens if the author forgot to assign a category. Is there a way to see if there is a category assigned or not while using a conditional like this?

2 Answers 2

9

I was able to accomplish what I was after using the following...

{if "{categories}{category_url_title}{/categories}"!=""}
    CATEGORY EXISTS
{if:else}
    NO CATEGORY EXISTS
{/if}
1
  • Super helpful, thanks @Brian! I extended your above example to display different Freeform headers {exp:freeform...} based on category_group: {if "{categories}{category_group}{/categories}"!="1"} INCLUDE THIS HEADER
    – Dan
    Sep 26, 2015 at 5:43
2

I use Switchee to check for the length of a string (e.g. for a change in design if title is too long). I suppose it could be adapted to check if the string is empty or not:

      {exp:switchee parse="inward" variable="{categories}{category_url_title}{/categories}"}
        {case value="full-page-review"}
          {embed="temp-group/.full_page_review" entry_id="{entry_id}"}
        {/case}
        {case value="standard"}
          {!-- just here as an example --}
          {embed="temp-group/.standard" entry_id="{entry_id}"}
        {/case}
        {case value="#^.{1,}$#"} 
        {!-- regex of ^.{1,}$ means match any character of 1 or more,
             I would have another number after the comma though. 
             You could just try #^.+$# instead, don't see why that wouldn't work --}
          {!-- unaccounted for case, but category exists --}
          {embed="temp-group/.standard" entry_id="{entry_id}"}
        {/case}
        {case default="Yes"}
          {!-- unaccounted for case, category DNE --}
          {embed="temp-group/.standard" entry_id="{entry_id}"}
        {/case}
      {/exp:switchee}

The above assumes that {category_url_title} will not leave whitespace before/after it.

2
  • I used my solution since it does not require the use of more addons. ;)
    – W3bGuy
    Dec 5, 2013 at 19:16
  • Yeah I just posted mine in case it might be of use to someone down the line (in the case they're looking for a string length limitation). With this you also won't have to use advanced conditionals if you want more checks when the category_url_title isn't empty.
    – notacouch
    Dec 5, 2013 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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