How to Add Different Sidebar Content For Each Page in WordPress

  • follow us in feedly
Published May 29, 2014 by Brad Knutson
Adding Different Sidebar Content to Each Page in WordPress

I’ve built my fair share of WordPress sites. My clients love WordPress because it makes updating their content extremely simple after our contract is up. Of all the project I’ve done and been asked to do, WordPress is by far the most requested.

For all that WordPress is, it does have it’s flaws. I’ve written about WordPress flaws before – if you’re interested in reading some of my past articles on WordPress, here they are. One flaw that is commonly overlooked is the inability to add unique sidebar content to each and every page and post. WordPress Sidebars are controlled by widgets by default, but when you add content to a widget and assign it to the sidebar, it will appear on every page that uses that widget area. My clients often want the ability to add different sidebar content on different pages, but out-of-the-box it can’t be done.

There has to be a way, right?

Luckily, there are a few. Which one you should use depends on your needs and your comfort level with PHP and working in WordPress templates.

I Require Different Sidebar Content On All Pages

In these situations, I use a couple of my favorites plugins.

Widget Logic

Widget Logic

This plugin gives every widget an extra control field called “Widget logic” that lets you control the pages that the widget will appear on. The text field lets you use WP’s Conditional Tags, or any general PHP code.

What makes Widget Logic so powerful is the ability to use WordPress’ Conditional Tags. Only a basic understanding of WordPress conditional functions is required.

For example, if you want a sidebar widget to only appear on the home page, the is_front_page() and is_home() functions can help you out. Here is a quick overview of what the “home page” actually is in the context of a WordPress blog (it can get a little confusing).

Here are a few more common examples:

Of course, this is called Widget Logic, so you can use any PHP operators you want to include or exclude Conditional Tags to really narrow down what page(s) you want your sidebar to appear on.

An extensive list of WordPress Conditional Tags can be found here.

Why would we stop there, however? The default WordPress Widget configuration leaves a lot to be desired. You either need to work directly in HTML or a WordPress template file. What about a WYSIWYG Widget?

WYSIWYG Widgets / Widget Blocks

Widget Blocks

Widget Blocks is the plugin that does just that. It allows you to create a “Widget Block” using the traditional WordPress WYSIWYG and add it to any sidebar, just like that.

The combination of these two plugins means you can have whatever content you want, on any page’s sidebar, with little to know WordPress programming knowledge. This is a very powerful combination.

I Require Different Sidebar Content On Some Pages, But Not All

Let’s say you have more than a handful of pages that need a unique sidebar, but the rest can all be the same. It might be a little overkill to use the Widget Logic and Widget Blocks plugins like the first example, so how about a method that is slightly less draining on server resources (after all, the more plugins you have, the more processing your server needs to do on every page load).

How about using a Custom Field to specify a sidebar template? Sure – add this snippet into your page template.

Now, whenever you want to call a different sidebar template, you add your Custom Field like so:

WordPress Custom Field

In this example, I chose ‘affiliate’, which will load the sidebar-affiliate.php template, instead of the standard sidebar.php template. If no custom field is specified, then the regular sidebar.php template is used. Pretty neat and easy solution, right? Just add whatever content you want into your new sidebar templates and call them by adding a custom field to the pages and posts that you need them on.

I Require Different Sidebar Content On One or Two Pages

Some of my clients are OK with having their sidebar content be consistent across all there pages. Some even request it. It’s not uncommon however to request to add unique sidebar content for a single page or two. Doesn’t seem unreasonable, right?

In these situations, I opt to register a new widget area and create a new WordPress page template.

Register the Sidebar Widget Area

In your functions.php, the following function will register a new widget area.

Replace the id, name and description fields with whatever makes sense for your sidebar. If you need two or more unique sidebars, you’ll need to register a sidebar widget area for each of them.

After registering the widget area, it should now show up in the Appearance > Widgets page for you to add content.

Create a New Page Template

I’ve written about creating new templates in the past, but I’ll quickly gloss over the subject again here.

Start off your template by creating a new PHP file and adding something along these lines to the header.

Create your HTML structure as you would any other WordPress template. When you get to the part about adding a sidebar, do one of two things.

get_sidebar() function

This is the basic function call. It’s essentially the include function in PHP and will include the template in your theme directory named sidebar.php.

If you have different sidebar templates, use this instead:

Instead of calling the sidebar.php file, the above function call will load the sidebar-custom.php file.

dynamic_sidebar() function

You can use the get_sidebar() function to keep things modular and clean, or you could skip it altogether. Instead, you could use the dynamic_sidebar() function.

The get_sidebar() takes the ID of the sidebar widget area that you registered previously. In that example, the ID was “about-page-sidebar”. This function displays the content you entered into the “About Page Sidebar” widget in the WordPress admin. Just like that!

What do you think? The 3 methods are very different from each other, and essentially accomplish the same thing, just in different ways. Which one would you prefer? Do you think you have a better way? Let me know in the comments below!

The following two tabs change content below.
Founder at Inbounderish
Brad Knutson is a Web Developer in the Twin Cities area of Minnesota. He has experience working with WordPress and Drupal, and also has an interest in SEO and Inbound Marketing.

Keep Up-to-Date

Subscribe

Topics

See a complete list of topics discussed in blog posts here.

Check These Out

Get 2 Weeks Free! Sign Up Today! Premium Managed WordPress Hosting Genesis Framework for WordPress SEO is complex. Tools should be simple. Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

2 thoughts on “How to Add Different Sidebar Content For Each Page in WordPress

  1. edvard

    Thank you for your post.

    However, it is not easy for me as not-a-programmer.

    How come there is no system/widget to install to have a custom sidebar per page, without having to use php ?

    Joomla have this :)

    Eddie

    Reply

Share Your Thoughts

Your email address will not be shown.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">