Tutorial: Different Sidebars for Homepage and Posts

This is an update with full illustrations to an older post.

Basic Method:

1) Find your sidebar.php file.

2) Make a copy of it. Name it sidebar-secondary.php. Your second, third, forth, etc. sidebars all need to start with sidebar-NAMEHERE.php. That's part of the naming convention in Wordpress.

3) Make the content changes you need in secondary sidebar file. This is where you make the secondary sidebar different. Whatever it is you need to be different should go in this file. Save sidebar-inside.php and upload it to your server.

4) Locate your single.php file.

5) Look for a line near the end of the file that looks like this: <?php get_sidebar(); ?>

6) Change it to <?php get_sidebar('inside'); ?> (note, the 'inside' part should match the name of the new sidebar-inside.php file). The single quote marks ' ' are required.

Should look like this:

7) Save your new single.php file and upload it.

8) Done.

You've now made two separate sidebars. You then told your single.php file (single posts) go fetch a different sidebar file than your index.php (homepage) does. Your homepage will pull in the old sidebar.php file. Your single posts will pull in the sidebar-secondary.php file.

Note: In your sidebar-inside.php file, you will want to remove the dynamic sidebar components. Otherwise, your homepage widgets will also show up on your inside pages. Look for a line like this:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>.

Only remove that line of code from the file if you do not need the new sidebar to have dynamic widget functionality. Otherwise, if you would like to wigetize this sidebar, proceed to the next lesson below.

If you would like to get a fully detailed version of these instructions with illustrations and screen shots, just fill out your name and email address below. We will send you the documentation instantly. It is far more detailed and contains useful screen shots for you to follow along.

How to make your new sidebar Widget Ready or Dynamic:

We have to do two things. 1) Register your sidebars to your Wordpress backend knows you have two ready for widgets. 2) Tell each sidebar.php page which set of widgets to pull in.

Here we go:

1) Find your functions.php file.

2) Look for the register_sidebar section. It will look something like this:

if ( function_exists('register_sidebar') )

register_sidebar(array(

'name' => 'Homepage Sidebar',

'id' => 'homepage_only',

'before_widget' => '<li id="%1$s" class="widget %2$s">',

'after_widget' => '</li>',

'before_title' => '<h2 class="widgettitle">',

'after_title' => '</h2>',

));

Illustration

It may not contain all of the lines such as 'name' or 'id'. It may not even exist at all. In the next step, you will overwrite the old version. If you don't have one, you will make one now.

3) Set number 1 will control the homepage sidebar and should be setup to look like this:

if ( function_exists('register_sidebar') )

register_sidebar(array(

'name' => 'Homepage Sidebar',

'id' => 'homepage_only',

'before_widget' => '<li id="%1$s" class="widget %2$s">',

'after_widget' => '</li>',

'before_title' => '<h2 class="widgettitle">',

'after_title' => '</h2>',

));

Illustration

'name' => 'Homepage Sidebar', will refer simply to the name of the sidebar that shows up on the Widgets admin page drop down list.

'id' => 'homepage_only', is a unique ID just for that sidebar set. We will add this ID to the sidebar templates later.

4) Next, make the second set for the inside pages:

if ( function_exists('register_sidebar') )

register_sidebar(array(

'name' => 'Inside Pages No Cal',

'id' => 'inside_pages',

'before_widget' => '<li id="%1$s" class="widget %2$s">',

'after_widget' => '</li>',

'before_title' => '<h2 class="widgettitle">',

'after_title' => '</h2>',

));

The above set is almost identical to set #1.  The only change is the 'name' and 'id'.  For the name of this sidebar, we decided to call it 'Inside Pages No Cal'.  You can call it whatever you would like.

5) STOP. This is how a section inside your functions.php file should now look:

6) Continue.

7) Locate your sidebar.php file.

8) Locate the line that looks like this:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>

9) Make it look like this:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('homepage_only') ) : ?>

Illustration

You have now told your single-inside.php file to go look specifically for the widgets assigned to the homepage_only function.

10) Locate your sidebar-secondary.php file.

11) Locate the line that looks like this. If none exists, don't worry.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>

12) Make the line look like this. If you do not have this line, simply add it in the possition where you want your dynamic widgets to appear.

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('inside_pages') ) : ?>

Illustration

You have now told your single.php file to go look specifically for the widgets assigned to the homepage_only function.

13) Upload your newly changed functions.php, sidebar.php, and sidebar-secondary.php files to your server.

14) Earlier in the basic method, we already changed your single.php file to pull in the sidebar-inside.php file. So at this point, you are all done.  If you did not do this, go back to Step 5 in the Basic Method instructions.

You can now log into your Wordpress Admin and go to the widgets section. You will now see a dropdown menu with your Homepage Sidebar and Inside Sidebar listed. You can add widgets separately to each sidebar. When you save, the changes will update on your site.

All done!  Hope this was helpful for you.

Visit http://blogcraving.com for more helpful tips, tricks, and money making ideas for your Wordpress blog!