Articles Anywhere allows you to use if-else structures inside the {articles}
tag.
The if structures tag syntax looks like:
{if ...}...{elseif ...}...{else}...{/if}
You can use pretty much any Data Type inside the if conditions. Especially the Numbers are very useful, as we have seen above.
Check if the data exists or not
You can check whether a data exists (meaning it's NOT empty or false) or not. For instance, to show the readmore tag only if the article has a fulltext, you can do:
{if fulltext}[readmore]{/if}
If you want to show a different readmore text when the article doesn't have a fulltext, you can do:
{if fulltext}[readmore text="Read full story"]{else}[readmore text="See intro"]{/if}
Or the opposite, you can place specific content if the data doesn't exist (meaning it's empty or false):
{if !fulltext}...{/if}
Check value of the data
Or check if a data is (or is not) equal to a certain value, like:
{if author-id = 62}...{/if}
{if title != 'Furry Animals'}...{/if}
Articles Anywhere's If Structures support these comparison operators:
=
Equal!=
Not equal<
Less than>
Greater than<=
Less than or equal to>=
Greater than or equal toIN
Value is found in the list of values. Example:{if 'Cats' IN tags}
or{if color IN ['blue', 'red', 'green']}
NOT IN
Value is not found in the list of values
Multiple Conditions
You can also place multiple conditions in one {if}
tag.
If all of the conditions should pass, separate the conditions with AND
or &&
.
If any of the conditions should pass, separate the conditions with OR
or ||
.
{if category = 'Cats' && 'furry' IN tags}It's furry and purry{/if}
{if habitat-area = "Asia" || habitat-area = "America"}It's Asian or American{/if}
Custom Fields PRO
You can also base your if conditions on custom field values, by simply using the custom field name - for example:
{if nr-of-legs}...{/if}
Normally when using {if}
tags with custom field values, the condition will check the raw value of the custom field (as saved in the database).
But in certain cases, such as lists or checkbox fields, you might want to check against the text value of the custom field instead. You can do so by adding :output
after the name of the field:
{if nr-of-legs:output}...{/if}
Nested If Structures PRO
Normally, you can only use one level of If Structures, meaning you can't nest If tags inside other If tags.
However, if you would like to use nested if structures, you can do so by taking advantage of the Nested Articles Tag.
You can simply wrap your nested set of {if}
tags in a nested {article}
or {articles}
tag. For example:
{articles category="Animals" include_child_categories="true" separator="<hr>"} <h4>[link][title][/link]</h4>
{if category = 'Cats'}
This is a cat.
{article-nested id="[id]"}
{if 'furry' IN tags}
It's also furry.
{elseif 'fluffy' IN tags}
It's very fluffy.
{/if}
{/article-nested}
{/if}
{/articles}
Examples
Some more useful examples of using If Structures:
- Output content based on dates values
- Output content based on the number of articles displayed
- Check even/uneven articles displayed
- Check whether an article is access-restricted
- Check whether an article is published or not
- Check whether an article is the current active article
For full control via PHP, you could also use Sourcerer to output the {articles}
tag depending on your custom checks.