• Feed RSS

PHP for WordPress: Mastering Conditional Statements and Tags

"The conditional statements feature is an extremely useful and powerful feature in WordPress; still it remains unused in the development process. You can achieve many things by the simple and smart use of these statements. Sometimes when you need specific text or a unique image to appear on a specific page and not on any other pages in your site, by using conditional statements along with WordPress conditional tags you can achieve this easily without creating multiple pages or templates.


What are Conditional Statements?

PHP “If” statements are generally known as conditional statements. These statements are used within WordPress Theme files along with certain WordPresss functions defining the logic to inform the WordPress database what and how the content should be displayed based on a given criteria. These statements are very important in defining WordPress themes and are similar to creating template hierarchy.
Basic Conditional Statements look like this:
<?php if ( ) { ?>......<?php  } ?>
In layman’s terms, this is pretty simple:
If “something” exists/happens/etc., Do something.
You can (and probably will) use this type of general “if” statement inside WordPress all the time. WordPress has it’s own set of conditional statements as well though, so let’s take a peek at what those look like:

A List of Important WordPress Conditional Tags

Different types of Conditional Tags exist within Conditional Statements. These tags fetch particular information from the WordPress database. These conditional tags are defined for many different items in WordPress e.g. posts, tags, texts, images, categories etc.
Some of the most popular ones are-
1.is_page() - If you want to apply a certain condition to one of your specific page, e.g. “Contact Us” page. You can use this tag to refer to that page using its database ID number or title or slug/name. For instance:
is_page(’2')
or
is_page(‘Contact”)
2. is_category() – If you want to apply a certain condition to a specific category page e.g. Books, then this tag can be used to refer to that page using its database ID number or title or slug/name. For instance:
is_category(“4”) 
3. is_home() – This is used to refer to your home page.
4. is_single() – This is used for single page blogs, single posts or attachments.
5. is_tag() – This is used to refer to a tag archive page. It works similar to category page.
6..is_archive() – This is used to refer to archived pages.
7. is_search() – This is used to refer to search results pages.
8. is_404() – This is used to refer to a HTTP 404: Not Found error page.
9. is_author() – This is used to refer to an author archived page.
10. is_comments_popup() – This is used to refer to a comment popup window.
You can get the complete list of tags on the WordPress Codex page.

Learning by Example

This is all fine and well in theory, but let’s dig into some practical code examples of these conditional statements in action.

Example #1:

What should be the code to display an image on your first page, nothing on the second and some text on the third page? (These pages are hypothetical, you can replace them with your own page names, like Contact Us, About, Information, etc…)
<?php  if ( is_page('First_Page') ) { ?>
 <img src="image.gif" />
 <?php  } elseif ( is_page('Third_Page') ) { ?>
 <p>Here is some text….</p>
 <?php  } else { ?>
 <?php  } ?> 
Please Note-
These codes should be written in your writable theme editor on the page.php file where you want the conditional content to appear.
Extra Note: This is a multi-conditional statement (see the multiple “if, elseif, else…” running logic?) This code checks for the appropriate page using the tags and then displays the items accordingly. You can use unlimited conditions within a code.
is_page(array(‘First_Page’,’Second_Page’)) can be used to display something on both the pages.

Example #2:

How to display a text on either a single post OR a particular category page?
Here you need to use the “||” symbol to display something if any one of the given condition is met. If no condition is satisfied, it displays nothing.
<?php  if(is_category(Category_Page) ) || ( is_single(Single_Page))  { ?>
 <p>Display  this text….</p>
 <?php  } else { ?>
 <?php  } ?>
 
Extra Note: We used the “||” here, which checks for any of the conditions… Alternatively, we could use the “&&” to create an AND condition, in which both the condition must be met in order to display the item. The “!” is used for excluding something from the list.
E.g. !( is_page(Excluded_PageName)). You can also use variables to refer to the sub pages of a parent page. E.g. post->post_parent=="Parent_Page_Name"

Example #3:

How to load other items like CSS files, Java Script files in one of my pages?
This code snippet will show you how to display contact form files only on your Contact Us page.
<?php  if ( is_page( 'contact_us_page' ) ) {   ?>
 <link  rel='stylesheet' href='/contactusform.css' type='text/css' media='all' />
 <script  type='text/javascript' src=k'/jqueryform.js'></script>
 <script  type='text/javascript' src='/spec_forms.js'></script>
 <?php  } ?>

Example #4:

How to use conditional tags in your custom post types?
The mixture of custom post types and conditional tags makes a smart way to display information to the users. The following code snippet will enable you to display contact information at the end of your review posts.
<?php  if ( review_entry' == get_post_type() ) { ?>
 <div  id="reviewcontact">
 <p>If  you're impressed by the  reviews, kindly  contact us asap. Give us a call or <a href="/contact"  title="Contact Us">CLICK HERE</a> to send us a message!</p>
 </div>
 <?php  } ?>

Example #5:

How to check if a “post thumbnail” image has been uploaded or not?
The following code checks for an image and display another if not found.
<?php  if(posted_thumbnail()) {
 show_thumbnail();
 }  else {?>
 <img src="<?php  bloginfo('template_directory');?>/images/default_Image.gif" alt="Image not Displayed">
 <?php  }?>

Conclusion

Thus by mastering these little conditional tags and mixing them with php conditional statements, you can write less and do more in your WordPress site. This not only helps you during the maintenance of your site but it also helps to take advantage of the highly useful WordPress database architecture.
If you guys want to see more of these “basic PHP” tutorials for WordPress, let us know in the comments!"