Working with conditional tags

Along with using page templates to display different content in different parts of your site, you can also use conditional tags. These are bits of PHP that tell WordPress to check if something is the case and, if so, run some code or, if not, to either run a different piece of code or to do nothing at all.

We'll scrape the surface of conditional tags here as it's a huge subject that you can do a great deal with, but it can get very complicated.

First, let's have a quick look at what conditional tags should look like.

Conditional tags' syntax

The basic syntax for conditional tags is as follows:

<?php         
if ( [condition] ) 
{ 
  // code to output when condition is met
} 
?>

If the condition isn't met, WordPress would just skip the contents of the conditional tag and go straight on to the next bit of code. We'll create an example of this shortly.

You can also check when a condition is not true, using an exclamation mark:

<?php         
if ( ![condition] ) 
{ 
  // code to output when condition is not met
} 
?>

Or you can use else to add an alternative action if the condition is not met:

<?php         
if ( [condition] ) 
{ 
  // code to output when condition is met
} 
else 
{
  // code to output when condition is not met
}
?>

These are the three basic ways of using conditional tags.

Incorporating conditional tags in our theme

Can you think of any examples where we've already used conditional tags in this book? For a start, there's our loop, which we've used in all of our template files. When looking for posts to output, WordPress checks if there are any posts to display, and displays them if this is the case; if not, it does nothing.

The code in the loop which does this is shown as follows:

<?php if ( have_posts() ) : ?>
  // code to display our post titles and content
<?php else : ?>
  /// code to output if there are no posts
<?php endif; ?>

Note

You may have noticed that this code is slightly different from what you've already seen in the loop, which uses while (have_posts). An if statement checks if there are posts, but a while statement just tells WordPress to do something while there are posts to display, and isn't used for checking conditions.

The else statement and its contents are optional, but it helps to have something there just in case WordPress can't find anything to display. In our search.php template, as you'll remember, we added some code to output a list of recent posts inside that else statement, so that it's only displayed if the search finds no results:

<?php if ( have_posts() ) : ?>
  // code to output posts if the search is successful
<?php else : ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
      <?php get_search_form(); ?>
      <h3>Latest articles:</h3>
      <?php $query = new WP_Query( array ( 'post_type' => 'post', 'post_count' => '5' ) );
      while ( $query->have_posts() ) : $query->the_post(); ?>
        <ul>
          <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
        </ul>
      <?php endwhile;   ?>
<?php endif; ?>

This means you've already been working with conditional tags and you didn't even know it! Now let's work on another example of using conditional tags – hiding the page title on the home page.

Using conditional tags to hide the home page's title

Let's imagine that we're working on a site which needs a static page as the home page instead of the blog listing we're using for Open Source Magazine. If we created a site like this we'd probably want to call our home page Home. But would users need to see that Home title on the home page? Not really, as they know it's the home page.

Let's try a really easy way to do this using CSS first, before moving on to using a conditional tag.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset