Like discussed earlier at this post, Some blogs pages look different from their regular pages or just the Blogger wants the Page to look different from the rest of the blog. They design Custom Page Templates for those pages. Its just the same case here. You want your Sidebar of a particular page to look different from the rest? Or just say a simple completely customizable widgetized sidebar for wherever you want to use it ? This tutorial also works if your theme as 1 sidebar and you want 2 or more instead.
Lets get going with the tutorial.
Creating Sidebar and calling into Wordpress:
All the functions of a theme are written in a file called functions.php. If your theme has only one sidebar, then most probably there will be no functions.php file in your theme folder. In that case, we are going to create the file ourself.
Creating functions.php
- Just open notepad or any other code editor to start a new file. Put this code into that file.
-
<?php
if ( function_exists('register_sidebars') )
register_sidebars(2);
?> - Save the file as functions.php
- and put it in your theme folder.
This code tells Wordpress to register two sidebars for you, see register_sidebars(2). If your theme is already using more than one sidebar. You will find the functions.php file already present in your theme folder. You just have to edit the number to your requirement and save the file. You can increase the number if you want more sidebars, and you think your theme layout can handle it. Now, when you go to your Wordpress admin section > Appearance and then Widgets, you will see a new sidebar in your dropdown menu. You can drag your widget items into any of the sidebars.
Creating Sidebars:
If your theme has only one sidebar, try to locate a file called sidebar.php in your theme folder. In this example, where we are trying to modify the theme for two sidebars, let’s rename sidebar.php to sidebar1.php and make a new blank file called sidebar2.php. Put this code into sidebar2.php and save the file
<div>
<ul>
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else : ?>
<?php endif; ?>
</ul>
</div>
So, we have the two sidebars ready but they have not been placed in the index.php file yet. Both these sidebars need to be called from the index.php file in order to include them in your theme. Just open the index.php file from your theme folder and locate the code that calls your sidebar file (sidebar.php earlier). It should look something like :-
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>
Edit this code and change the words sidebar.php to sidebar1.php.
This takes care of the first sidebar. Now take a look at the index.php file carefully and find a suitable place to insert the second sidebar. This might involve modifying your layout or adding new divs. Once you find a suitable place, place the following code there.
<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>
Save the index.php file and now preview your theme. You will see all the widgets that you placed in both your sidebars appearing on your website. If you have not placed any widgets yet, you will not see any change. There might be alignment errors but you will have to fix them yourself.
Using Custom Sidebar for Particular Pages:
Above We just told how to add another sidebar if you already have one. Here I’m going to tell you what to be done incase you want your new sidebar at only particular pages. Lets say you have a custom Page Template called “CustomPage.php” and you want this new sidebar for the page instead of default one.
Its as simple as:
- Open your CustomPage.php
- Find any of the code like
- <?php get_sidebar(); ?> OR
<?php include (TEMPLATEPATH . '/sidebar.php'); ?> - If the code is <?php get_sidebar(); ?> replace it with
<?php include (TEMPLATEPATH . '/sidebar2.php'); ?>you created above. - If the code is
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>just change the sidebar.php filename to sidebar2.php.
Was this tutorial usefull to you? Am I missing something ? or just a Hello maybe ?
Comment now, We love it !





April 8th, 2009 at 9:20 am
Great post, Enk
Personally I’m a complete noob to coding, but I want to learn so that I can code WP themes. Seems like a great hobby.
Look forward to more similar posts
[Reply]
April 8th, 2009 at 9:25 am
So, its like you liked it again..
To be very frank, I was also noob but tweaking Wordpress themes again and again made me understand some of the code
. Otherwise I myself don’t know xhtml or php. Just a little knowledge.
Keep on reading articles like that, all around the web. If you are really passionate, you’ll learn pretty fast.
[Reply]
April 8th, 2009 at 9:31 am
Ah, cool idea, will definately try that out. Just from making small changes to HTML things I’ve learnt the basics. Yeah, I’ll carry on reading.. nice advice
[Reply]
April 8th, 2009 at 9:57 am
Keep on trying starting from small changes.. You’ll do lot better later on.
Keep visiting !
[Reply]
April 8th, 2009 at 5:41 pm
The only time I had to make changes to sidebar was to remove sidebar on some of the pages on my blog. I hardcoded the ‘page ids’ in a IF statement to use an empty sidebar. I just didn’t call ‘get_sidebar’ function
[Reply]
April 8th, 2009 at 6:24 pm
yeah thats cool and pretty easy. Bother writing a Guest Post with this tutorial ?
Anyways, Happy to see you here. Keep visiting !
[Reply]
April 9th, 2009 at 1:20 am
Sure, I tried to send you an email but it bounced.
Can you send me a test email (you can get my email from comments).
[Reply]
April 9th, 2009 at 6:35 am
Enk, my emails bounced when I tried to send you things, too. Thought it was just me but obviously not… you should really get that fixed
lol
[Reply]
April 9th, 2009 at 6:45 am
I’ve sent an email to both of you, You guys can contact me there.
In the mean time, I’ll try to fix the issue.
Thanks for telling
@Jeet,
Get yourself a nice Gravatar dude. There’s a link for signup at the bottom of the comment-form, you can try that.
[Reply]
April 9th, 2009 at 3:49 pm
Thanks. The combined mail caused some confusion
I didn’t see Simon’s message first.
Not keen on Gravatar yet
[Reply]
April 9th, 2009 at 8:19 pm
As you wish, it was just a suggestion. Gravatar gives you identity.. many people see Gravatars before reading the comment, and many remember commentators by Gravatars too. At the end, the decision is always yours.
[Reply]
April 11th, 2009 at 4:26 pm
Kewl post Enk
[Reply]
April 11th, 2009 at 4:27 pm
Glad you liked it Farhad.. Keep coming back
[Reply]
April 12th, 2009 at 9:24 am
I know basic coding, yes that sounds funny considering I run a free themes site – I do outsource though.
I know how to move stuff around and add other stuff, but cannot code a whole theme! – Nice tut btw, should help me out
[Reply]
April 12th, 2009 at 9:28 am
Same case is with me Daniel. I can play with WP and its Themes, do this and that. But can’t just create from scratch.
But I’ll be getting near, slowly.
[Reply]
April 14th, 2009 at 3:07 am
Thats a great tutorial. I am trying to get bit by bit, deeper into Wordpress. There are so many great resources on the net.
As of lately, I always start by using a framework then customizing it to my tastes.
[Reply]
April 14th, 2009 at 8:15 am
Thanks Bryan, Nice to see you here. Its all like hit and try technique, do it again and again and finally you’ll know to do it the correct way and the better.
Hope to see you here again, Bryan.
[Reply]
November 20th, 2009 at 4:26 am
thanks for the useful tips. now dont hv to search and sort for custom sidebar themes.
just can work on any theme with some tweaking trick from ur post.
thanks for the informative post
[Reply]