Learn how to write wordpress if statements

Knowing how to write WordPress if statements can help you take your websites to the next level. They are meant to pose a question and based on the answer to that question you can do or show something. This can be a really powerful tool in your development work belt, especially with the conditional tags WordPress already has built into the core. In this tutorial, I am going to give you the basics of WordPress if statements and provide a few real world examples.

Structuring WordPress If Statements

As you explore the world of conditional logic, you may see if statements written in two different forms. As far as I know, both of them work the same way but the second example below is supposed to be a quicker/shorter format. Feel free to choose whichever structure you like best.

Example One
<?php
if ( ) {

   the_content();

}
else {

   the_excerpt();

}
?>
Example Two
<?php if ( ): ?>

   the_content();

<?php else: ?>

   the_excerpt();

<?php endif ?>

Exploring The Code

In both of the two examples about, the code can be read like this: IF SOMETHING – show the content – ELSE – show the excerpt. The SOMETHING part is determined by one of the conditional tags provided by WordPress. I will show you a few examples later so that this portion makes more sense.

WordPress if statements can be expanded to include elseif statements:

<?php if ( ): ?>

   the_content();

<?php elseif ( ): ?>

   the_title();
   the_content();

<?php else: ?>

   the_excerpt();

<?php endif ?>

This simply adds another condition and now reads: IF SOMETHING – show the content – ELSEIF SOMETHING – show the title and the content ELSE – show the excerpt.

WP Conditional Tags

I have already provided the link above to the WordPress codex page listing all of the available conditional tags and I highly recommend you study and memorize this list. These conditional tags will make all of the above code make sense. Let’s look at the conditional tags is_home. This tag will result true if the page you are viewing is the page that lists the most recent (usually 10) posts. So if you have your home page displaying the recents posts, which can be set under the Settings -> Reading tab on your dashboard, then the home page of your website will result true. As WP developers, we can use these if statements in our template files to show different content based on the page you are viewing.

A Verbal Example

The page.php file in your development structure is the template that every single one of your pages will use on your website. Let’s say you want to have a custom tagline written on a couple of the pages. In this case, you can use an if statement that says if you are on the about page say “something” but on the services page say “something else”.

The Code Example

Let’s looks at what the example above would look like in code:

<?php if ( is_page('about') ): ?>

   <h1>Something</h1>

<?php elseif (is_page('services') ): ?>

   <h1>Something Else</h1>

<?php else: ?>

   <h1>This will show on any other page that isn't About or Services</h1>

<?php endif ?>

In the parenthesis after the if, you pass in a parameter in this case the conditional tag is_page and then you pass in the slug of the page into the is_page parameter. You can also pass in the ID of the page in the parenthesis of the is_page conditional tag if you would rather. Each conditional tag usually accepts a few different parameter format choices, such as you can pass in the ID, title of the page or (like in this example) the slug.

Final Words

The more WP sites you built the more you will understand how WordPress if statements can be utilized in your projects. Conditional tags give you the ability to make your template files more dynamic and can help you build some pretty impressive functionality into your projects. I encourage you to go out and look for other real world examples out there.