EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts
ExpressionEngine mini-howtos created by the EE Insider community.
Conditionally display text if an entry has categories assigned to it
An entry may have zero, one or multiple categories assigned to it.
Imagine you want to put something like this below your entries:
Filed under: Books, Fantasy, Novel
Of course you don’t want to display an empty
Filed under:
if there actually aren’t any categories.
Since {categories} in {exp:weblog:entries} is a pair-variable you cannot use it like single variables in conditional-statements.
{categories}Filed under: {category_name}{/categories}
would only work when there is exactly one category assigned, otherwise the “Filed under: ” would repeat as well. Also there is no {count}-variable available inside the loop that could be used to conditionally limit output to the first iteration.
So what do we do? The answer is amazingly simple. We use a second instance of the variable pair and set it to limit=“1”. The text in between will only show up once, no matter how many categories are assigned to the entry. If there are no categories it won’t display at all.
{categories limit="1"}Filed under: {/categories}
{categories backspace="1"}{category_name}, {/categories}
Email
Print
Post to Twitter
Post to Delicious
Add Your Comment?
You must have an EE Insider account to post comments on Insider Tips. It's fast, easy and hassle-free.
Sign up now (or login).