Create a different sidebar for a blog page

Sometimes we want to display different sidebars depending on what type of page is being viewed. This can be a great way of serving targeted content to your site visitors, and involves very little coding. This tutorial, which uses the popular Revolution Music theme, shows you how to create a specific sidebar which will only be displayed when viewing the single blog post page.

Of course, you don’t have to have a copy of the Revolution Music theme to follow this Code Snippets tutorial, as the principles are the same regardless of your theme. If you are not using the Revolution Music theme, all you need to do is to find the corresponding code in your theme files and go from there!

Revolution template structure

The Revolution Music theme has 4 sidebar files, used as follows:

  • sidebar_home.php – only used on the home page
  • sidebar.php – used on all pages apart from the home page
  • sidebar_left.php – “sub” sidebar, shown on all pages
  • sidebar_right.php – “sub” sidebar, shown on all pages

The single blog post is displayed using index.php.

Make a new sidebar

Let’s make the new sidebar, which we want to display when viewing a single blog post page, ie when index.php is used to display a single article or blog entry.

Step 1: Make a copy of sidebar.php

Step 2: Rename the copy sidebar_single.php

Step 3: Then, open up your single blog post file, in this case index.php, and find this line of code:

and change this code to:

That’s it! Your single blog post page will now call our newly created sidebar file, sidebar_single.php, instead of the original sidebar.php. Remember! The only change we have made is to the single blog post page. The home page, archives and static pages are unaffected by this change.

Customising the new sidebar

Well that’s all fine and dandy, but sidebar_single.php is exactly the same as the original sidebar.php that it replaces, and there’s not much point in making the new sidebar unless we customise its content.

In the case of the Revolution Music theme, the standard sidebar.php displays a misture of hardcoded content – ie content that is coded directly into sidebar.php such as the video, newsletter etc – and dynamic content – ie widgets.

Let’s say we have some widgets which we only want to display in sidebar_single.php. To do this we need to make a couple more coding changes to our theme files.

Step 4: Open up sidebar_single.php and find this line of code:

and change this code to:

All we’ve done here is change “1” to “4”. Why? The Revolution Music theme has 3 widgetised sidebars:

  • sidebar.php – dynamic_sidebar(1)
  • sidebar_left.php – dynamic_sidebar(2)
  • sidebar_right.php – dynamic_sidebar(3)

As you can see, each widgetised sidebar has a unique reference number which is shown in the “dynamic_sidebar” code. We need to give the “dynamic_sidebar” code in the new sidebar a unique reference number, which is why we’ve edited this code in sidebar_right.php and used the number “4”.

Step 5: The next thing we need to do is to “register” this new widgetised sidebar in the theme’s functions.php.

Step 6: Open up functions.php and find this code at the top of the file:


and change it to this:


WordPress now knows that there are 4 widgetised sidebars for this theme and the new sidebar will now appear in Dashboard>Design>Widgets as "Sidebar 4". Important note: always be careful when editing a functions.php file - make sure that there is no whitespace at the top of the file!

That's it! We now have a customised sidebar which can accept it's own set of widgets and which will only be displayed when viewing the single blog post page (index.php).

Further ideas

The method described above is fine for adding one or maybe two sidebars. However, for more complex situations, there are probably more elegent ways of doing this type of customisation, such as with Conditional Tags. A topic for another article, I think...

About this article:
This article is one of an occasional series covering customisations of specific themes. More Revolution based tutorials to follow!
Related articles:
If you found this article useful you may also like these articles from Code Snippets:
Complex loop with in_category(): Part 1
Creating author pages
How to widgetise a theme file Coming soon!

Comments

  1. I really like this and believe after reading the well written instructions I am going to attempt to change a sidebar on my forum page. I would like to add a sidebar that shows my affiliate providers. Not sure it will work with my width, but want to see! Thanks for the great post!

  2. Thanks for your nice comment, Candace! Glad you found the article useful.

    If you run into any problems, feel free to post a question on the forum.

  3. Excellent How-To, well written and easy to follow adding studiograsshopper to my live feeds folder. Off now to do some sidebar pimping.

  4. @ Countzero

    Thanks very much! Glad you enjoyed the article. 🙂

  5. Like it.

  6. awsome …. i.ve used what u writed here :X 10x

Leave a Reply to Ade Cancel reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*


5 × = thirty