Two Sidebars with Widgets for WordPress 2.8
August 17, 2009
I needed to make a second sidebar for a WordPress theme – starting from a simple one-sidebar theme. It is a three-step process to get a sidebar recognized by WordPress so that it shows on your screen. Also, each sidebar must have its content listed in a separate file, like sidebar.php.
First, you have to tell WordPress that there are two sidebars for your theme. This step is called registering the sidebar. The important point here is to make sure that each sidebar is given a different name. Here, two sidebars are designated. They will have the names sidebar 1 and sidebar 2. Put this code in functions.php.
<?php
if (function_exists("register_sidebars")) {
register_sidebars(2, array("name" => "sidebar %d"));
}
?>
Alternatively, the functions.php code could use separate lines, one for each sidebar, like so:
<?php
if ( function_exists(‘register_sidebar’) )
register_sidebar(sidebar-one);
register_sidebar(sidebar-two);
?>
You could have more than one sidebar. Just make sure each one is registered with a new name.
The content for each sidebar will be in separate files, such as sidebar1.php and sidebar2.php.
Second, each sidebar must be included in the index, page, or single pages where each sidebar is to appear. Use a template path in an include statement with the name of each sidebar file, like so:
<?php include (TEMPLATEPATH . ‘/sidebar1.php’); ?>
<?php include (TEMPLATEPATH . ‘/sidebar2.php’); ?>
Third, the sidebar.php files must pass the name of the sidebar when calling the dynamic_sidebar function. At the top of each sidebar.php file, insert this code:
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(’sidebar 1′) ) : ?>
Here, we pass the name of the sidebar (sidebar 1), not the filename (sidebar1.php).
The top of the sidebar1.php file is:
<div class="sidebar">
<ul>
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(’sidebar 1′) ) : ?>
followed by a few list-items, closing unordered list and closing div tags. Sidebar2.php is as follows in its entirety:
<div class="sidebar2">
<ul>
<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(’sidebar 2′) ) : ?>
<?php endif; ?>
</ul>
</div>
There you have it! Now all there is to do is to go back to the WP admin panel > appearance > widgets and select the widgets that you need for each sidebar. Now, your WordPress theme has two sidebars!
Posted in
content rss
