I purchased a PHP book over 6 years ago and I only got through the first 20-30 pages before putting it down and letting it gather dust. After six long years, I finally faced a problem big enough for me to actually write a piece of code:
How to display two or more different WordPress sidebars (the column on the right) depending on the page a user is currently viewing?
The Problem
WordPress currently displays a single sidebar for all your templates (sidebar.php). My problem was that I wanted to use another sidebar (let’s call it sidebar2.php) as well. More specifically, I wanted this second sidebar to be displayed only on ‘Single Post‘ (the template that displays the full blog post; the webpage you are viewing now). Then, the first sidebar would be displayed for all the other pages.
Being the newbie programmer I am, I tried using the original sidebar include code that WordPress uses to display the sidebar in your templates:
<?php get_sidebar(); ?>
And then modified it:
<?php get_sidebar2(); ?>
Unfortunately, this did not work.
The Solution
Luckily, I stumbled upon this useful page at WordPress about ‘Conditional Tags,’ specifically this section.
Basically, it tells us that ‘Single Post,’ the page you are currently viewing, is defined by WordPress as ‘is_single()’. This is the page where I wanted to display the second sidebar. This also means that for every other page that was not ‘is_single()’, I would simply use the original sidebar include code to display the first sidebar as mentioned earlier.
Thus, a simple PHP if and else conditional statement will do the trick. I replaced <?php get_sidebar(); ?> from header.php (or wherever this code is included in your template) with:
<?php
if (is_single())
include (TEMPLATEPATH . ‘/sidebar2.php’);
else
get_sidebar();
?>Note: The above code assumes that you have created and uploaded a WordPress template called sidebar2.php
In layman’s terms, if the current page you are viewing is ‘Single Post,’ then the second sidebar will be displayed. Else, the first sidebar is displayed for all other pages. This piece of code can be adapted in many different ways including displaying more than 2 sidebars and displaying sidebars for specific templates.
I guess this is a rather simple fix, but after hours of Googling for a solution, I noticed that a lot of other WordPress users experienced this same problem. Hopefully this article helps!

