If

The If tag evaluates whether a condition is true or false.

The basic structure is:

<If subject comparison value>
  The condition is true.
</If>

When the condition is true, its inner content is displayed.

Attributes

The condition is given in the tag attributes, in the following order:

  • Subject (Required)

    Subject of the condition.

    This can be given as the first atttribute without value, for example:

    First loop item

    <If first>

    Loop count is more than 2

    <If count more_than value="2">

    Subject with value

    Subjects like check and field require a value.

    Field is not empty

    <If field="title" exists>
  • Comparison (Optional)

    This defines what to do with the subject, such as compare to another value, or only check if it exists.

    It is given as compare="something", or second attribute without value.

    For example, is, more_than, less_than.

    If not given, the default is exists.

  • Value to compare - value="something" (Optional)

Core conditions

These are defined as conditions for WordPress core content. Loops and Logic add-ons provide conditions for additional content types.

  • archive=".." - Archive page - Accepts optional value of: category, tag, taxonomy, post (default), author, and date; Use attribute "type" or "taxonomy" to filter by post type and taxonomy, respectively

  • check=".." - Check value

    Optional attribute value of type text

  • count - Current loop count

    Optional attribute value of type number

  • field=".." - Field

    Optional attribute value of type text

  • first - First item in loop

  • last - Last item in loop

  • list=".." - List variable type

  • logic=".." - Logic variable type

  • previous_total - Previous loop items total

    Optional attribute value of type number

  • route=".." - URL route

  • singular=".." - Single post, page, attachment, or any post type; Use attribute "type" to filter by post type

  • total - Loop items total

    Optional attribute value of type number

  • user - User

  • user_field=".." - User field

    Optional attribute value of type text

  • user_role - User role

    Optional attribute value of type text

Common comparisons

  • exists
  • not_exists
  • is
  • is_not
  • any_is
  • all_is
  • any_is_not
  • all_is_not
  • more_than
  • more_than_or_equal
  • less_than
  • less_than_or_equal
  • starts_with
  • any_starts_with
  • all_starts_with
  • ends_with
  • any_ends_with
  • all_ends_with
  • in
  • not_in
  • includes
  • not_includes
  • any_includes
  • all_includes
  • any_not_includes
  • all_not_includes

Else

The Else tag is used to provide a template for when the If condition is not met.

Note that it is a closed tag. Everything after it will render when the condition is false.

<If field comparison value>
  The condition is true.
<Else />
  The condition is false.
</If>

Else if

Use Else if to check multiple conditions.

<If first_condition>
  First condition is true.
<Else if second_condition />
  First condition is false.
  Second condition is true.
<Else />
  All conditions are false.
</If>

Next: Logic variable type