Sales and Marketing Roadmap

WordPress: Using a different sidebar for static pages

And now for something completely different. Lessee if I can earn some techy bone fides.
Like many blogs, my sidebar is filled with blog-related functionality, e.g., search box, RSS, feed, blog navigational tools, etc., that isn’t particularly relevant to my static pages. So I wanted to use a different sidebar for the static pages.   If you look at my sidebar on this page, it includes typical blog sidebar stuff.  But if you go to the  “Marketing Help” page, you’ll see a completely different sidebar!
Easy enough, I suppose, for WordPress experts and easy enough to ascertain, for the technically-gifted. It took me a little while, however, to navigate through the steps and so I thought I would share. Again, I am no expert, so I can’t say for sure I’ve done it the best way or the “right” way, but it works for me and I think these steps are pretty easy. This assumes, by the way, that you are hosting WordPress yourself. These instructions might also vary by WordPress version and by theme.
So here goes:

1) Create a copy of your existing sidebar.php file, that lives in this directory: [domain]/wp-content/themes[your theme name]/ (You can do this with an ftp or shell client.)
2) Name the copy something useful, but keep “sidebar” in the name and include a “dash” after “sidebar.” I called mine “sidebar-custom.php” (You can do this with an ftp or shell client.)
3) You have to register this new sidebar, so you must edit your functions.php file. (You can do this inside wp-admin, by going to Appearance | Editor and choosing the functions.php file.)
4) Inside functions.php, you will likely find reference to the register_sidebar function. Specifically, look for:

if(function_exists(‘register_sidebar’)) {
register_sidebar(array(
‘name’ => __(‘Sidebar’),
‘before_widget’ => ”,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”
));

You want to add a reference to your new “sidebar-custom.php” file.  So after the “));” above, add:

register_sidebar(array(
‘name’ => (‘custom’),
‘before_widget’ => ”,
‘after_widget’ => ”,
‘before_title’ => ”,
‘after_title’ => ”
));

Notice that it’s the same, except I’ve named this sidebar with the name of the file after “sidebar-“, i.e. the root name.  Be sure you have a closing bracket, “}” after this last bit, which closes the “if statement” shown above. Update the file.
5. If you now navigate to your WordPress Widgets management page within wp-admin, Appearance | Widgets, you should see a second sidebar on the right hand side labeled whatever you called your custom sidebar. Mine is simply called “custom.” You can now drag widgets to the new sidebar. But, you’re not done yet. : )
6. Go back to the theme editor, Appearance | Editor and find your new sidebar-custom.php file and open it.
7. This file controls what widgets will appear in this sidebar. Most widgets these days are “dynamic,” in that they are built on the fly when requested by the user browsing your pages. Inside this file, look for the line:

if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(1) ) : ?>

This line essentially says “if the function ‘dynamic_sidebar’ doesn’t exist OR if the default sidebar.php file doesn’t exist, then display the static text that follows,” which is followed by html code that will be displayed. My template was actually written in the inverse, which said “if the function DOES exist AND the sidebar file DOES exit, then do nothing, otherwise write the static code. I like the version above better, so changed to that.
Change the dynamic_sidebar value to dynamic_sidebar(‘custom’), so the line looks like:

if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘custom’) ) : ?>

where ‘custom’ is the root name again you used in the new sidebar filename.  Update the file.
8.  Now, where do you want to put your new sidebar?  I wanted to leave my posts alone, but change the sidebar for just my pages.  So the final step is to open the page.php file in the Appearance editor and change <?php get_sidebar(); ?> to <?php get_sidebar(‘custom’); ?>.
Update the file and you should be all set.
There are all sorts of functions you can use to more precisely control where to put the sidebar.  See here.
References:
Customizing Your Sidebar
Function Reference/dynamic sidebar
Function Reference/register sidebar
Two widgetized sidebars
How to Create new Widget Area

2 Comments

  1. Mike

    Here’s a nifty plugin I just created called the Graceful Sidebar Plugin. This enables you to create custom content in the sidebar for each page or post. Simply install the plugin, activate it and then create two custom fields in your pages or posts called “graceful_title” and “graceful_content”. Then drag the graceful sidebar widget to your theme’s sidebar and you’re good to go.
    Enjoy!

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *