23 Apr 2010

WordPress: Conditional Tags Test

Conditional Tag test

Having trouble identify or determine what conditions that a page matches?

Add the following Conditional Tags test snippet to your WordPress Theme’s template file, preferably your Theme’s template sidebar.php file:

<?php
$conditions = array(
  'is_home()',
  'is_front_page()',
  '$wp_query->is_posts_page',
  'is_page()',
  'is_single()',
  'is_singular()',
  'is_attachment()',
  'is_search()',
  'is_archive()',
  'is_category()',
  'is_tag()',
  'is_tax()',
  'is_author()',
  'is_day()',
  'is_month()',
  'is_year()',
  'is_time()',
  'is_date()',
  'is_paged()',
  'is_comments_popup()',
  'is_preview()',
  'is_404()',
);

echo '<h2>Conditional Tag</h2><ul>';
  $i = 0;
  foreach ( $conditions as $condition ) :
    $condition = str_replace('()', '', $conditions[$i]);

    switch ( $condition ) :
      case 'is_front_page' :
        $query = is_front_page();
        break;
      case '$wp_query->is_posts_page' :
        $query = $wp_query->is_posts_page;
        break;
      default :
        $query = $wp_query->$condition;
        break;
    endswitch;

    echo '<li><code>' . $conditions[$i] . '</code>';
    echo $query ? ': <span style="color:green">true</span>' : ': <span style="color:red">false</span>';
    echo '</li>';

    $i++;
   endforeach;
echo '</ul>';
?>

Note: Currently, there’s no is_posts_page() conditional tag yet.