Posts tagged with “sidebar”

Creating a second sidebar for WordPress

Sunday, 6 December, 2009

wp sidebar editorNote: If you cannot do this on your own because you don’t have the needed knowledge, you can hire studio nocturnart to do the dirty work for you for a small charge. Just use the contact form!

Sometimes, one WordPress sidebar just isn’t enough! for example, on the nocturnart.com website which is used as a CMS I need a generic sidebar for the CMS part and another one for the blog functionalities like archives, categories, meta etc. Who knows, maybe I will need more at some point. In this case, with a little bit of tweaking the PHP, we can add multiple sidebars to our WordPress powered website and use them as needed. Here’s how:

Step 0: Locating or creating the functions.php file

First of all, we need to view and edit our functions.php file which registers the sidebars. This file must be under /[wordpress_directory]/wp-content/themes/[our_theme_name]/functions.php. If you don’t find it there, that means it does not exist for the theme we are using. Create one locally, open it and paste this inside it:

<?php
if ( function_exists('register_sidebars') )
register_sidebars(2);
?>

Now save the file and upload it. All done? Good, go to step 2. If there was a functions.php file proceed to step 1.

Step 1: The sidebar registration

If there was a functions.php file we need to edit it. We can do that simply by using the WP theme editor (make sure you have given writing permissions to the file). You can also download it and edit it with any text editor (notepad for Windows, Dreamweaver or gedit for Linux -  you gotta love gedit!). In the file we need to find this:

if ( function_exists('register_sidebars') )

Found it? Good. Take a good look at the code after that. Look for:

register_sidebars(2);

If this call exists you are almost all set, this means you already have 2 sidebars registered and you need to work on the second one. Go to step 2. If you don’t see these lines of code paste:

<?php
if ( function_exists('register_sidebars') )
register_sidebars(2);
?>

Step 2: Building the sidebars as we want them

At this point we should have two sidebars registered. Take a look at your theme files (by now you should know where they are loceted). Do you see something like sidebar2.php or another name that implies a second sidebar? You are actually done! Just use the Appearance -> Widgets menu, select the second sidebar and add/remove content! Then either call sidebar.php or sidebar2.php or whatever they’re called in the theme files you need to edit, using

<?php include (TEMPLATEPATH . '/sidebar.php'); ?> or <?php include (TEMPLATEPATH . '/sidebar2.php'); ?>

If there is no file implying a second sidebar, create one, and paste this inside it:

<div>
<ul>
<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(2) ) : else : ?>
<?php endif; ?>
</ul>
</div>

You may need to tweak that code depending on your theme structure. Now upload and again use

<?php include (TEMPLATEPATH . '/sidebar.php'); ?> or <?php include (TEMPLATEPATH . '/sidebar2.php'); ?> in your theme files.

All done! Use the widget editor to add and remove widgets to your sidebars!