(Please note that I’m assuming you have some experience editing your WordPress templates before you read this article.)
While creating the KurtSchweitzer.com website/blog I wanted to have sidebars that were both dynamic (meaning I can edit them via the control panel without writing any code) and different for different portions of the site. Why? Well, for one thing I wanted the home page to be relatively advertising free, but the posts need to have ads on them so I can make some money from my writing.
WordPress provides a mechanism to do this, but I found the documentation somewhat confusing. This post is my attempt to document what I did that actually works.
First I needed to edit functions.php in my template. The default template has this file, but some others don’t. (I created my own template by copying the default into an new directory and then editing it.) Near the top of the file is this code:
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
I changed it to this:
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
register_sidebar(array(
'name' => 'posts',
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
register_sidebar(array(
'name' => 'pages',
'before_widget' => '<li id="%1$s">',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));
}
What this does is tell WordPress that I want three sidebars. They will be in the files sidebar.php, sidebar-posts.php, and sidebar-pages.php. (The only parameter that’s really important is the ‘name’ parameter. It determines which files to use.)
Next I made copies of sidebar.php and called them sidebar-posts.php and sidebar-pages.php.
The third step is to edit sidebar-posts.php. Change this:
<?php /* Widgetized sidebar, if you have the plugin installed. */
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
into this:
<?php /* Widgetized sidebar, if you have the plugin installed. */
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('posts') ) : ?>
What you are doing is adding the name of the sidebar (‘posts’) to the dynamic_sidebar() function call. Do the same thing (with ‘pages’ instead of ‘posts’) to sidebar-pages.php.
Two more steps.
Next you need to edit the various page and post templates. Change
<?php get_sidebar(); ?>
to
<?php get_sidebar('pages'); ?>
in those you want to use the ‘pages’ sidebar, and to
<?php get_sidebar('posts'); ?>
in those you want to use the ‘posts’ sidebar.
(I left the default sidebar so that I don’t need to change every template, just the ones that use one of the special sidebars.)
After you’ve saved the edited files (functions.php, sidebar-pages.php, sidebar-posts.php, and the changed page and post templates) in the template directory, fire up your WordPress control panel and go to the Widget control panel. You’ll now see three sidebars – Sidebar 1, pages, and posts – on the right side. Drag and drop the widgets to configure each one, and then check your site and see how your pages and posts look!
I really like what you wrote here – it’s informative. Thanks for posting this. I’ve been experimenting with WordPress lately. Do you use WordPress? Any tips for me? Click here if you’d like to check out my site. Have a fun week!
You must know by now, your piece goes to the core of the issue. Your lucidity leaves me wanting to know more. Just so you know, i will forthwith grab your feed to keep up to date with your site. Saying thanks is simply my little way of saying great job for a marvellous resource. Take On my dearest wishes for your incoming article.