You can easily create a custom theme template to have alternate sidebar layouts.
This post is part of 30 WordPress hacks in 30 days.
Whether you’re installing a new WordPress theme or creating one from scratch, changing the sidebar seems to be one of the first things you do. Like most things in WordPress, you are only limited by your imagination. Different themes have different sidebar layouts. You can have one sidebar, or two, or even three or four. You can have one sidebar on the left, or right, two on the left or right, or one on each side.
How WordPress Pages Work
If you’re going to learn how to hack up your sidebar – I have to teach you how pretty much all WordPress pages function first.
From the WordPress Codex, let’s look at the world’s simplest WordPress index page –
Basically (in code) this says, “get my header, get my content, get my sidebar, and last get my footer”. Those are pretty much the 4 major areas of every WordPress page. Also, as I previously said – those 4 areas can be defined literally any way you want. WordPress uses a templated system, and the reason that it uses “get_sidebar” in every single templated page is so it gets the SAME sidebar content for each and every page. That way you define a sidebar (or header, or footer) ONE TIME, and every page on your blogs shows it the same.
Where is MY sidebar file?
By default “get_sidebar” gets the “sidebar.php” file from your theme directory. As of versio 2.5 of WordPress, you can also specify a particular sidebar file to be included. That’s probably not the case with your theme, most of the WordPress themes available were created before 2.5, and they usually have one sidebar file that includes additional sidebars either by div tag or another included file.
How is a sidebar coded?
Like I said before, when WordPress runs “get_sidebar” it looks for the file “sidebar.php” in your theme directory. That file then usually contains blocks of code to retrieve sections of information, like your blogroll, categories, archives, pages, etc. Each block is laid out in (html) “list ordered fashion” nearly always something like this –
Each theme styles sidebars and sidebar sections differently, with different font sizes, backgrounds colors, borders, spacing, and more in the theme stylesheet – but each block in the sidebar is written in that same code format.
What About sidebar widgets?
Widgets were added in WordPress 2.x so people wouldn’t have to muck around in the code anymore. A “widget” is basicly just a block of code that does something. Like using a widget to display categories, pages, or a search box. Widgets are great, because you can just choose the ones you want to enable and “save”. To use widgets in your sidebar, your sidebar.php (or other sidebar files) has to be “widgitized”. Read “widgitizing themes if your theme isn’t already widgitized.
How do I hack my sidebar layout?
There are two extremes to this. Most people would just change their theme until they find the page and sidebar layout they want. Web designers and coders would probably just rewrite the theme HTML and CSS code to their liking. Most people I know are somewhere between the two. They don’t want to change their entire theme, but they aren’t techie enough to be able to rewrite all the code themselves. To go around hacking in your sidebar code – I’m going to assume that you already have at least basic HTML and CSS skills.
This post isn’t about adding blocks of content or widgets to your sidebar, it’s about hacking the layout of your sidebar(s). For instance, I’m a big fan of the three column layout, with the content on the left and 2 sidebars on the right. Awhile back I installed a forum on this blog, and the main content area wasn’t big enough to accomodate it. I wasn’t about to change my theme just to change the sidebar layout…so this is what I did.
The first thing I did was download a copy of my “page.php” file.
Next I added the code to the top of the file to turn it into a template file –
Then I removed this line –
and I replaced it with just the code to display the left sidebar. I saved it as “forum-template.php” and uploaded it to my theme directory. Then in my WordPress dashboard, I went created a new page and used “Forum Template Page” for the page template. Once I save this page, I will have created a custom template page for my forum that just displays my left sidebar. This opens up the content section of the page to be large enough to accomodate width of the forum. I could have used the right sidebar instead, or just had no sidebar at all (only a header and footer).
You could create multiple template that use an alternate sidebar structure, and “include” those pages by using code like this –
just change the name of the file to the one you’re using. You can have as many template files and as many sidebar files as you want, I would just keep it to a minimum so you can keep track of them all.
Have you created custom sidebary layouts? Comment now!






Okay, I so needed this post. LOL.
I love this series and greatly appreciate your time and effort in all these neat little tricks. I literally printed them out and stuck them in my wordpress binder. (Can’t you tell that I’m a committed developer! LOL!)
@Erika – thanks for your thought! It’s comments like this that keep me writing more!
Hi. I copied page.php and named it newblogpage.php. I placed in the code at the top to indicate a template file. The theme has two sidebars. I removed the call to r_sidebar.php, and kept in the call to l_sidebar.php.
I set my blog page to use the New Blog Page template.
Unfortunately, it didn’t work. Both sidebars are being displayed. Did I follow the instructions correctly?
@Lana – In the top of “newblogpage.php” what did you call the template? Is that what you set your blog page to use as a template? Open “newblogpage.php” and place some garbage text like adfadf adsfadf adsfa after the “if posts have posts” section and re-upload to your theme directory. Do you see the garbage text on your page, and do you still see the 2 sidebars?
I called the template New Blog Page. Yes, I set my blog page to use that template. I added the garbage text, and it didn’t appear. Both sidebars are there.
But I did another test. I created a new page and selected the New Blog Page template. The garbage text and only one sidebar appears. So there seems to be a glitch when calling in the blog posts.
@Lana – now I see where the problem might have been. This hack is for WordPress “pages”, not for posts. You said it didn’t work for a post (which it shouldn’t have) – but did for a “page” – so it was working as it should have, you just didn’t know it.
It actually is a page that lists the blog posts. So I guess calling in the posts messes up the page code.
Do you know how to set up pages to have their own sidebars, and the blog with its own sidebar? Many premium themes are set up this way. Here’s an example:
http://essence.ithemes.com/
http://essence.ithemes.com/hello-world-2.htm
If it’s just a page that lists the blog posts then you did something wrong. To setup pages that have their own sidebars, just do what I indicated in the article…remove the call the “get_sidebar” and then replace it with the php include at the end of my post above. Write your new sidebar(s) in separate files and call them in the template by using the includes.
Hi. I’ve followed your instructions, but it’s still not working. Based on the following from the Codex I think there may be conflicts (and more than having both home.php and blog.php files, which I’ve tested as well). This problem is beyond my abilities! I happened to pick a theme that is hard for me to decipher.
http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder
I appreciate your instructions for creating page templates and sidebars. Those work fine. =
@Lana – I think you may have picked either a really old or badly coded theme. Nearly every WordPress theme I’ve ever used, “index.php” was what was used for the homepage. It’s not necessisarity more correct, it’s just what I see more of. You can use “home.php”, but I’ve never heard of blog.php being used, and that’s not even listed in the WordPress Codex Template Hierarchy. If you look at that hierarchy you’ll see that in your situation “home.php” is called first, then page.php, and index.php last.
In other words – you kinda solved your own problem here. The hierarchy shows what order WordPress “looks for” theme template files. If it finds “home.php” (which your theme has), it uses that for the blog home and doesn’t move on. That’s why when you assign another page – it doesn’t honor your settings it uses home.php instead. To fix this, just delete both home.php (and blog.php) fro your theme directory, and in your settings set your home page to be your newtemplatefilename.php and it should work just fine.
Notice that in the future (for additional templates you make), the index.php file is always the last one WordPress looks for in the hierarchy of calls.
I hope this helps and solves your problem. It may have take time – but look how much more you’ve learned about WordPress themes in the process!
Thanks! But I’m still confused, sorry to say =)r
I deleted home.php and blog.php.
When I installed this theme, I created a page called Home. That is what is set to be the Home page. Are you saying I need to create a template to be the Home page? If so, which file do I base it on?
Also, I’m still stuck about how to have the blog posts use a different sidebar.
I don’t want to take up your valuable time, so let me know if I overstay my welcome here!
(About the theme, I think it’s fairly new. And the developer is creating premium themes, so I thought the code would be good.)
Ok, yes – create a page in the WordPress dashboard called “home” (like you have), and then create a theme template page, called “home-template.php” or whatever. Base it off index.php if you have one, or you could always re-download a fresh copy of your theme and base it off the “home.php” you deleted since that was your previous home page.
If you want to have different sidebars on the homepage using that template – they you need to remove the call for “get_sidebar” in the template file and replace it with a PHP include (example at the end of this post above) that calls an alternative sidebar file (that you’re going to create). You make a copy of your “sidebar.php” and save it as “home-sidebar.php” or something and then that’s the file you include when you remove the call to “get_sidebar”.
does that help?
Hi. I greatly appreciate your explanations, they’re very easy to understand.
Using wp 2.6, I’ve been playing with multiple sidebars, widgetizing them, etc. I have a theme in development that uses the get_sidebar(). I wanted to have some bottom widgetized sidebars so a client could customize the area. I edited the functions.php file, created two more sidebar files (sidebar-left.php and sidebar-right.php). Now, in the templates on the pages where I want these bottom sidebars, I have the include statements as you explained. But, I also have the original get_sidebar() because I still want that to appear on those pages. It’s all working, but I was wondering if there is an issue with having both the get_sidebar() and the includes together. Thanks.
there shouldn’t be any issue using get_sidebar and includes together at all. The includes just include whatever code is in that file – so there’s no conflict at all. =;;
Right, that was my thinking, thanks. I was wondering also if there’s a difference between this:
“ and this
“
I’ve seen both used and wondered if there’s a “right” way to do it or if it depends on the theme.
I think my code was left out of my comment. I surrounded by backticks.
The two lines were php:
“include(‘sidebar-file.php’)” and
“include(TEMPLATEPATE . ‘/sidebar-file.php’)”
I forgot to mention that I’ve read the Codex about templates, http://codex.wordpress.org/Stepping_Into_Templates, but for newbies, it may not be very clear. So, perhaps it’s just a good idea to use the absolute path when using includes (?)
I would use the first one, although if the second one works it’s just another way to do it. =0
@Robyn – you don’t need to use an absolute path with includes, just go from the root, like “/myfile.php” or “/wp-content/themes/mytheme/myfile.php”
This post helped tons. Thanks for your efforts and time.
Peaveys last blog post..Ban the Pixie Stix!
Hi, I’m building a website for my school and there are a lot of pages. Currently, my wordpress page has a very long list of pages in its side bar and I am trying to organize it.
My pages are built in hierarchy way and i was wondering if there is anyway to have different sidebars for the different levels. (main page, sub pages, sub-sub pages). I know you can individually code it but I have over 1500 pages so I was hoping there was another way.
I hope this makes sense.
Thanks
Web Student
Hi,
I stumbled onto this post when trying to figure out how to change up a template’s layout. I’m trying to move the sidebar from the right to the left of the page. Can anybody enlighten me as to how this can be done?
nobody can really explain this, it’s a matter of how your theme is setup and HTML code. If you don’t know how to do this – likely you’ll need to hire somebody.
Oh, this is fantastic. I wanted to put a 35×70 google chat plugin image on the right side in my horizontal nav bar. I also wanted a random image plugin working in the header. Using your method here, I was able to achieve that. How fun being able to ‘widgetize’ any part of your theme. Thx