PHP If/Else

Conditional container for template logic

Template Mode Only

This element is only available when editing Site Templates (tcb_template posts). It does not appear in the Elements panel when editing regular pages or posts.

Overview

The PHP If element creates a conditional container that renders its children only when a specified condition evaluates to true. This is essential for building dynamic templates that display different content based on WordPress state, custom fields, or user permissions.

PHP If is a container element that can hold other elements as children. These children are only rendered when the condition is met. You can extend the conditional logic by adding PHP Else or PHP Elseif elements inside the PHP If container to handle alternative conditions.

The condition can be any valid PHP expression that evaluates to true or false, including:

  • WordPress conditional tags (is_single(), is_page())
  • Variable comparisons ($count > 0)
  • Function checks (has_post_thumbnail())
  • Custom field existence (get_field('show_section'))
  • User capability checks (current_user_can('edit_posts'))

Settings

Configure the PHP If element using these Inspector options:

Setting Description Default
Condition The PHP expression to evaluate. If it returns true, the child elements are rendered. Do not include the if ( and ) : parts — just the expression itself. $condition

Condition Examples

Here are common condition patterns you can use:

Condition When True
$variable === true Variable equals exactly true
$count > 0 Count is greater than zero
!empty($items) Items array is not empty
is_single() Viewing a single post
is_page('about') Viewing the "about" page
has_post_thumbnail() Post has a featured image
get_field('show_section') ACF field is truthy (non-empty)
current_user_can('edit_posts') User has edit permissions
is_user_logged_in() User is logged in
have_posts() WordPress query has posts

PHP Else

The PHP Else element provides an alternative branch when the parent if condition is false. It must be placed as a direct child of a PHP If element.

Else Settings

Setting Description Default
Children Elements to render when the parent if condition is false. Drag elements inside the Else container. (empty)

PHP Else has no configurable settings — it simply renders its children when all preceding conditions (if and any elseifs) are false.

PHP Elseif

The PHP Elseif element adds additional conditional branches to an existing PHP If. Use it when you need to test multiple conditions in sequence.

Elseif Settings

Setting Description Default
Condition The PHP expression to evaluate if the parent if condition (and any preceding elseif conditions) are false. $condition

You can have multiple PHP Elseif elements within a single PHP If, each testing a different condition. They are evaluated in order until one matches.

Examples

Simple Conditional

Show content only when a condition is true:

<?php if (has_post_thumbnail()) : ?>
  <!-- Featured image element here -->
<?php endif; ?>

If/Else Structure

Show different content based on a condition:

<?php if (is_user_logged_in()) : ?>
  <p>Welcome back!</p>
<?php else : ?>
  <p>Please log in to continue.</p>
<?php endif; ?>

If/Elseif/Else Structure

Test multiple conditions in sequence:

<?php if (current_user_can('administrator')) : ?>
  <p>Admin Dashboard</p>
<?php elseif (current_user_can('editor')) : ?>
  <p>Editor Dashboard</p>
<?php elseif (current_user_can('author')) : ?>
  <p>Author Dashboard</p>
<?php else : ?>
  <p>Subscriber View</p>
<?php endif; ?>

WordPress Loop with If/Else

A complete WordPress loop example showing conditional thumbnail display:

<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
    <?php if (has_post_thumbnail()) : ?>
      <?php the_post_thumbnail('large'); ?>
    <?php endif; ?>
    <h2><?php the_title(); ?></h2>
    <p><?php the_excerpt(); ?></p>
  <?php endwhile; ?>
<?php else : ?>
  <p>No posts found.</p>
<?php endif; ?>
Quick Tip

Use PHP If with have_posts() to wrap a PHP While loop. This pattern displays content when posts exist and shows a "no results" message via PHP Else when the query returns nothing.

Next Steps