If you have ever noticed. Some Blogs have some pages different than others.. like one wants to show Ads on homepage but not Inner (Article) pages. You may want to do that too and you wonder How can we do it ?
Well, this tutorial is for you. Here, I’m gonna tell you how to Create custom Widgetized Sidebar which you can used on specific or any pages you want.
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 yourself.
- Creating functions.php
- Just open notepad or any other code editor to start a new file. Put this code into that file.
- Save the file as functions.php
- and put it in your theme folder.
This code tells WordPress to register two sidebars for you, seeregister_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.
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 calledsidebar2.php. Put this code into sidebar2.php and save the file
<?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(2) ) : else : ?>
<?php endif; ?>
So, we have the two sidebars ready but they have not been placed in the index.phpfile 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.