Who has ever made a complicated WordPress setup bump to the problem: how can I make different sidebars to my pages in my theme? I bumped it of course and I found a very simple solution for it. It is very flexible and needs only a basic level of PHP programming so I think anybody can do this with his/her WP theme.
OK, let see the problem and then the solution.
The problem
Normally there is only one sidebar.php in WordPress which you can found in the wp-content/themes/your-theme-name directory. This sidebar.php can be called with the get_sidebar() function. In the default WP setup you can find this function calling in the single.php (displaying a single post), in the page.php (displaying a page) and in the index.php (listing posts). You have only one sidebar.php to modify -if you like- in your WP setup. There are many reasons to use more than one. For example: you have a different front page than your post displaying page, or you would like to differentiate your posts and pages, or you would like to build up a whole site based on the WP very good content management system (CMS).
The solution
I do not like to mixed up the original WP’s operational mechanism thought that I like to upgrade my WP with new versions. Keep this fundamental thing in my mind I make a small modification only in my page.php. I did not bother the single.php and the index.php because these files was good for me with the original sidebar.php.
First of all I made two copy of the sidebar.php. I renamed the first to sidebar1.php and the second to sidebar2.php (I do not want to use my brain capacity to guess a cool name
)
Then I modified the page.php a bit. Here is the code and than I will give you a explanation.
Comment out the get_siderbar() row in your page.php file and insert this code after it.
switch(true) {
case is_page('contact'): //Contact
include(TEMPLATEPATH."/sidebar2.php");
break;
case is_page('projects'):
include(TEMPLATEPATH."/sidebar1.php");
break;
case is_page('services'):
include(TEMPLATEPATH."/sidebar1.php");
break;
case is_page('about'):
include(TEMPLATEPATH."/sidebar1.php");
break;
case is_page('privacy-policy'):
include(TEMPLATEPATH."/sidebar1.php");
break;
case is_page('sitemap'):
include(TEMPLATEPATH."/sidebar1.php");
break;
default: get_sidebar();
break;
}
I do not like to use many ugly if statement so I use a switch. In the switch statement I used the value true to jump in it. Then I used the is_page() WP function to check the pages where I want to use my different sidebars. You can make a very simple to understand code with the is_page() because in the parameter you have to use your page’s or post’s slug. Slug is your shorthand name of your post which makes your site URLs SEO friendly.
The default value of the switch is the get_sidebar() which will call our original sidebar.php.
I think it is very easy and nice. Do it with your themes if you need!
Post me a comment if you use my mod I’m very interested it was easy to implement to others or not.

Leave a comment: