What do you think?
Different Sidebars, Headers or Footers on Pages in WordPress
A request that I often get from clients, is that they want different pages to have different sidebar content in their WordPress websites. Setting this up is fairly straightforward and a spin-off benefit is that you get to see how easy it is to have different headers and footers on your pages too.
First of all, what follows is based on a self-hosted WordPress website. You will need to be able to edit your theme files and create new ones.
Setting Up New Sidebars For Your WordPress Site
Most WordPress site themes come with at least one sidebar file. It is usually called sidebar.php and somewhere in the code, it contains a line like this:
<?php if (function_exists(‘dynamic_sidebar’) && dynamic_sidebar(‘Sidebar Widgets’)) : else : ?>
This basically tells the file to load your chosen widgets into the sidebar, or do whatever follows the line of code. If you want to have different widgets or different content on different pages, you are going to need to have more than one sidebar to load into the page.
To start off, most of the page files (page.php, single.php etc) usually call on the sidebar.php file, and load it in to the page with this code:
<?php get_sidebar(); ?>
If you want to load a different sidebar, you would change it to something like this:
<?php get_sidebar(‘ads’); ?>
In this case, we are loading a sidebar file that is in our theme called sidebar-ads.php, so we need to create this file. The easy way to do this is to duplicate your existing sidebar.php file and rename it to sidebar-ads.php. Change the function_exists code above so it reads:
<?php if ( !function_exists(‘dynamic_sidebar’)
|| !dynamic_sidebar(‘Ads sidebar’) ) : ?>
<?php endif; ?>
Now you have a new sidebar file and it is set to dynamically load whatever widgets you put into it. But, if you want unique widgets or content, you now need to create a new widget zone for the new page.
Creating New Widget Zones
In your theme folder, you need to open up the functions.php file. In between the php tags you need to ‘register’ your sidebars. You can add code like this:
// Declare sidebar widget zone
if (function_exists(‘register_sidebar’)) {
register_sidebar(array(‘name’ => ‘Ads sidebar’,
‘id’ => ‘ads-sidebar’,
‘description’ => ‘This is the sidebar for all pages that show ads.’,
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’
));
}
Here, we tell our theme to add a new widget zone named ‘Ads sidebar’, the same as in our newly-created sidebar-ads.php file. We give it an id for styling with CSS, wrap the content in a div and set the titles in our widgets to level 2 headings.
That’s all there is to it. If you want to create multiple sidebars, just repeat the process above, and for multiple widget zones, add to the code block above like this:
// Declare sidebar widget zone
if (function_exists(‘register_sidebar’)) {
register_sidebar(array(‘name’ => ‘Ads sidebar’,
‘id’ => ‘ads-sidebar’,
‘description’ => ‘This is the sidebar for all pages that show ads.’,
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’
));
register_sidebar(array(‘name’ => ‘News sidebar’,
‘id’ => ‘news-sidebar’,
‘description’ => ‘This is the sidebar for all pages that show news stories.’,
‘before_widget’ => ‘<div id=”%1$s”>’,
‘after_widget’ => ‘</div>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’
));
}
A Note About Widgets
Whilst I have been talking about widgets in sidebars, they can also go in headers and footers. Many themes have widget-ready zones in these locations and you can do the same thing in your themes.
Custom Headers and Footers
There may be times when you have a page or two on your site that needs something different in the header or footer, such as a block of text, an image or a widget-zone. You have already seen above how to create multiple and unique sidebar pages, by appending to the file name. Well, you can do the same thing to create unique header.php or footer.php files, such as header-ads.php or footer-news.php etc. When you want to load them in to a page or template, simply do this:
<?php get_header(‘ads’); ?>
Told you it was easy!
other page
It's good to talk!