PHP Switch

Multi-branch conditional 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 Switch element creates a multi-branch conditional that tests an expression against multiple possible values. This is cleaner than multiple if/elseif statements when you need to match a single value against many options.

PHP Switch is a container element that holds PHP Case and PHP Default children. Each case defines a value to match against, and the case's children are rendered when there's a match.

Common use cases include:

  • Displaying different layouts based on post type
  • Rendering content variations based on ACF field values
  • Handling different post formats (standard, video, gallery, etc.)
  • Conditional styling based on category or taxonomy

Settings

Configure the PHP Switch element using these Inspector options:

Setting Description Default
Expression The PHP expression whose value is compared against each case. This is typically a variable or function call that returns a string, number, or boolean. $variable

Expression Examples

Common expressions to switch on:

Expression Use Case
$type Custom variable containing a type value
get_post_type() Current post type (post, page, product, etc.)
get_field('layout_style') ACF field value for layout selection
get_post_format() Post format (standard, video, gallery, quote, etc.)
get_option('theme_style') WordPress option value

PHP Case

The PHP Case element represents a single branch within a switch statement. When the switch expression matches the case value, the case's children are rendered.

Case Settings

Setting Description Default
Value The value to match against the switch expression. Can be a string (quoted), number, or constant. (empty)
Has Break Whether to include a break; statement after the case content. Set to false to allow fall-through to the next case. true

Case Value Examples

Common values for case matching:

  • 'post' – String literal (include quotes)
  • 'page' – String literal
  • 'product' – Custom post type
  • 1 – Integer
  • true – Boolean
  • 'video' – Post format
  • 'gallery' – Post format

PHP Default

The PHP Default element provides the fallback branch when no case values match the switch expression. It's the equivalent of else in an if/elseif/else chain.

Default Settings

Setting Description Default
Has Break Whether to include a break; statement after the default content. Usually not needed since default is last, but included for consistency. true

Examples

Post Type Switch

Display different content based on post type:

<?php switch (get_post_type()) : ?>
  <?php case 'post': ?>
    <article class="blog-post">
      <!-- Blog post template -->
    </article>
    <?php break; ?>
  <?php case 'page': ?>
    <article class="page-content">
      <!-- Page template -->
    </article>
    <?php break; ?>
  <?php case 'product': ?>
    <div class="product-card">
      <!-- Product template -->
    </div>
    <?php break; ?>
  <?php default: ?>
    <article class="default-content">
      <!-- Fallback template -->
    </article>
    <?php break; ?>
<?php endswitch; ?>

ACF Layout Style Switch

Render different layouts based on an ACF field:

<?php switch (get_field('layout_style')) : ?>
  <?php case 'full-width': ?>
    <div class="container-full">
      <!-- Full-width layout -->
    </div>
    <?php break; ?>
  <?php case 'sidebar-left': ?>
    <div class="grid grid-cols-4">
      <aside class="col-span-1"><!-- Sidebar --></aside>
      <main class="col-span-3"><!-- Content --></main>
    </div>
    <?php break; ?>
  <?php case 'sidebar-right': ?>
    <div class="grid grid-cols-4">
      <main class="col-span-3"><!-- Content --></main>
      <aside class="col-span-1"><!-- Sidebar --></aside>
    </div>
    <?php break; ?>
  <?php default: ?>
    <div class="container">
      <!-- Default centered layout -->
    </div>
    <?php break; ?>
<?php endswitch; ?>

Post Format Switch

Handle different post formats with unique templates:

<?php switch (get_post_format() ?: 'standard') : ?>
  <?php case 'video': ?>
    <div class="video-post">
      <div class="aspect-video">
        <!-- Video embed -->
      </div>
      <h2><?php the_title(); ?></h2>
    </div>
    <?php break; ?>
  <?php case 'gallery': ?>
    <div class="gallery-post">
      <div class="grid grid-cols-3 gap-2">
        <!-- Gallery images -->
      </div>
      <h2><?php the_title(); ?></h2>
    </div>
    <?php break; ?>
  <?php case 'quote': ?>
    <blockquote class="quote-post text-2xl italic">
      <?php the_content(); ?>
    </blockquote>
    <?php break; ?>
  <?php default: ?>
    <article class="standard-post">
      <?php the_post_thumbnail(); ?>
      <h2><?php the_title(); ?></h2>
      <?php the_content(); ?>
    </article>
    <?php break; ?>
<?php endswitch; ?>
Switch vs If/Elseif

Use PHP Switch when comparing a single value against multiple options (post type, format, style). Use PHP If/Elseif when testing different conditions that don't share a common expression.

Fall-through Behavior

Setting Has Break to false allows fall-through, where multiple case values can share the same content. This is useful when several values should trigger identical output.

Next Steps