Using Conditionals In Your Theme Sidebars
One of the easiest ways to customize the look of your blog is by placing conditionals in your sidebars. Using conditional tags avoids having every page of your blog look exactly the same way. And, using them is an opportunity to practice editing your WordPress theme.
Editing WordPress Themes With Conditionals
Conditionals basically use Else If logic to determine when PHP coding should be executed. In other words, when you want a particular plugin to display. There is actually a wide variety of coding styles being used by different bloggers. The coding styles being suggested in this post will work in a wide variety of situations.
The following is your all purpose If Else Conditional statement.
<?php if (Condition X) { ?>
<?php execute_plugin_W(); ?> // PHP coding
<br> // Elements of HTML coding
<?php execute_plugin_X(); ?>
<?php } else { ?>
<?php execute_plugin_Y(); ?>
<?php } ?>
After each condition, multiple lines of coding can be executed. Lines preceded by// are comments that do not execute.
WordPress Conditional Tags
WordPress provides the test conditions that can be tested for by your basic Else If Conditional statements as follows.
is_front_page() // The better generic approach
is_single() // Is it a Post?
is_sticky() // Returns true if "Stick this post to the front page." Added with Version 2.7.
is_page() // Is it a Page?
is_page() && $post->post_parent) // Is it a Sub-Page?
is_page_template() // Added with Version 2.5
is_category()
has_tag() // Added with Version 2.6
is_search()
is_404()
is_paged() // Page that is split up over multiple pages.
// The following are different types of archives.
is_tag()
is_archive()
is_author()
is_date()
is_year()
is_month()
is_day()
There is little point in me explaining in depth about each of these conditions, when the Conditional Tags documentation provided by WordPress is excellent.
There are two other variations of the basic Else If Conditional statement.
<?php if (is_front_page()) { ?>
<?php execute_plugin_W(); ?> // PHP coding
<br> // Elements of HTML coding
<?php execute_plugin_X(); ?>
<?php } elseif (is_page()) { ?>
<br> // Elements of HTML coding
<?php execute_plugin_Z(); ?>
<?php } else { ?>
<?php execute_plugin_Y(); ?>
<?php } ?>
And …
<?php if (is_front_page()) { ?>
<?php execute_plugin_W(); ?> // PHP coding
<br> // Elements of HTML coding
<?php execute_plugin_X(); ?>
<?php } ?>
The Logical OR & AND
The following is how you do a logical OR conditional statement; which tests for when it is either the home page, a post, a page, or a category.
<?php execute_plugin_W(); ?> // PHP coding
<br> // Elements of HTML coding
<?php execute_plugin_X(); ?>
<?php } ?>
The following is how you do a logical AND conditional statement which tests for the second or higher page number of the home page, but NOT for the first page of the home page.
<?php execute_plugin_W(); ?> // PHP coding
<br> // Elements of HTML coding
<?php execute_plugin_X(); ?>
<?php } ?>
Using Conditionals In WordPress Is Easy
Where there it is, everything that anyone will ever need to know about writing conditionals all in just one post. Following the advice given in this post should save you many hours of headaches from trying to search for this information on the Web.
I will leave you with an actual functioning example for a plugin that was covered in a previous post, which uses a simplified IF conditional format.
This basic conditional should be used for all plugins. If a plugin were ever disabled, then there would be an error that would result in your sidebar aborting at that point. Hence, you should always first check for the existence of a plugin function before trying to execute it.