submit to reddit

Wordpress Tutorial: Create Post-Specific Sidebars Using Custom Fields

Looking for a way to add more sidebars to your Wordpress blog? Want to be able to create and assign a unique sidebar to one individual blog post? Take this easy 3 step tutorial and change the dynamics of your WordPress forever. Learn how to create not only one dynamic sidebar, but as many as you want. Have sidebar content targeted to individual posts using custom fields.

Up until recently I was at peace with my WordPress theme sidebar setup. However, as I added more content and some posts started to receive more traffic than others, I felt a growing need for post-specific and individually targeted sidebar content. Using three separate tutorials I managed to create a new sidebar for one specific post publication, and customize the sidebar for optimal Adsense earnings and visitor interaction. Now I am making a staggering $3 a week on Adsense, a 300 % increase, thank you very much!

Step 1 - Registering new sidebars

It is in functions.php the sidebar is 'registered'. All you need to do now is register a few more. Simply copy the code snippet located in functions.php, and displayed below, and paste each snippet for as many sidebars that you want.

Locate the initial snippet in functions.php:

if ( function_exists('register_sidebar') ) {
            register_sidebar(array('name'=>'sidebar1',
                   'before_widget' => '<li id="%1$s" class="widget %2$s">',
                   'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',        
                   'after_title' => '</h2>', ));


Copy the code displayed below and paste it below the initial snippet as many times equal to the amount of new sidebars you want.

register_sidebar(array('name'=>'YourSidebarName',
                  'before_widget' => '<li id="%1$s" class="widget %2$s">',
                  'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',
                  'after_title' => '</h2>', ));

Make sure the bold code is present (if it's not, add it) and that you set the unique sidebar name for each code snippet you add. Note also that the code within the register_sidebar snippet may vary from theme to theme, and that the important thing is that you include the bold code.

Example of code:

if ( function_exists('register_sidebar') ) {
            register_sidebar(array('name'=>'sidebar1',
                   'before_widget' => '<li id="%1$s" class="widget %2$s">',
                   'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',        
                   'after_title' => '</h2>', ));

register_sidebar(array('name'=>'movies',
                  'before_widget' => '<li id="%1$s" class="widget %2$s">',
                  'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',
                  'after_title' => '</h2>', ));

register_sidebar(array('name'=>'wordpress',
                  'before_widget' => '<li id="%1$s" class="widget %2$s">',
                  'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',
                  'after_title' => '</h2>', ));

register_sidebar(array('name'=>'music',
                  'before_widget' => '<li id="%1$s" class="widget %2$s">',
                  'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">',
                  'after_title' => '</h2>', ));
}

When you have added the registration code into functions.php, go to the admin>appearance>widgets, and your new sidebars should appear like below:

Step 2 - Creating sidebar files

Now, like the parent file sidebar.php, your newly registered sidebars also need to exist as files. Open the parent sidebar.php and copy the code over to a new html document. Save it where sidebar.php is located, as sidebar-YourSidebarName.php.

Add the code below, above <?php dynamic_sidebar(); ?> in sidebar-YourSidebarName.php and remember to match the sidebar name in the code (bold) with the file name.


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

 
Note! Make sure to save the new sidebar as sidebar-YourSidebarName.php, with emphasis on "sidebar-". Once this is done, the new sidebar files will appear in admin>appearance>editor, like below:

Step 3 - Adding the Custom Fields function

Finally, you just need to implement the custom field function. Locate <?php get_sidebar(); ?> in single.php and replace it with the following code:


<?php $sidebar = get_post_meta($post->ID, "sidebar", true); get_sidebar($sidebar); ?>





This will allow you to decide upon a sidebar using Custom Fields for each individual post. This however does not mean that you have to make a unique sidebar every time you write a new post. Leave it, and the original sidebar will be used.

Add the new custom field 'sidebar' and in value type in the name of the sidebar you want (using tutorial examples: movies, wordpress or movies). Do not put the whole file name of your new sidebars, such as "sidebar-movies", as it will not work. Excluding "sidebar-" will suffice.  You can now start to fill each sidebar with post-specific content, ads etc.

Make it stable

Ok, now you (hopefully) know how to add post-specific sidebars, making you a millionaire. All kidding aside, this know-how is a good thing bit it won't do you any good if your WordPress site isn't stable. The three providers below all have WordPress hosting plans that we think will satisfy most peoples' needs.

Feedback

If you find this tutorial too hard and/or impossible to complete with desired results, chances are it's you, me or your blog situation. In any case, please comment below or send me an email to brian [at] webhostingsearch [dot] com. Also you who has found this tutorial helpful are welcome to share your kind words :).

Add Your Thoughts

Gravatar

Commented by: wordpress video tutorial, 11 January, 2010

Great tutorial for wordpress users. Thanks.Really I hellp this post.

Gravatar

Commented by: wordpress video tutorial, 11 January, 2010

Great tutorial for wordpress users. Thanks.Really I hellp this post.

[url=http://www.wordpress-r-us.com/]wordpress tutorial[/url]

Gravatar

Commented by: Olivia, 19 January, 2010

What a lifesaver! Thank you for explaining everything in detail, I found another tutorial that gave me the same gist as this but left out too many details and I couldn't figure out why it wasn't working. I owe success to you, thanks for this!

Gravatar

Commented by: brian franklin, 22 February, 2010

glad it helped, it's mashup of two three separate tutorials :)

Gravatar

Commented by: Phillip, 28 March, 2010

I have been able to successfully add the sidebar to individual posts but it is not in the format of my theme. How do I get it to be in the format of my theme? Currently I am using Arthemia. Thanks so much for your help!

Gravatar

Commented by: Gail, 19 May, 2010

Where can we see an example of the extra sidebars? Please, and thank you.

Gravatar

Commented by: Lovina, 1 June, 2010

I am looking for this information and across so many blogs but only this who provide the real step by steps guide. My each posts now customized with unique sidebars.

Thank you so much for the great tutorial

Gravatar

Commented by: Mandy, 21 June, 2010

I'm using the editor under appearance. I can't seem to get past step 2, as I don't know how to create a new html file or save it. Was that ability removed in Wordpress 2.5? I'll upgrade to 3.0 and see if that helps.

Gravatar

Commented by: Kristina, 7 July, 2010

I'm trying to do this with the Create Your Own WP Theme tutorial-I can register the new sidebars but they're not appearing with my theme. Am I missing something?

Gravatar

Commented by: Carol, 8 July, 2010

Thanks a lot for yet another first-rate WordPress tutorial. I am always on the look-out for awesome WordPress tips to suggest to my readers. Thanks for taking the time to write this post. It's exactly what I was searching for. Truly great post.

Gravatar

Commented by: Carol, 15 July, 2010

I want to thank you for yet another first-rate WordPress tutorial. I am always trying to find super WordPress tips to

suggest to my readers. Thank you for posting this article. It's just what I was searching for. Truly amazing post.

Gravatar

Commented by: David, 1 September, 2010

Bless You!! FINALLY, someone who understands what we want when we Google "adding sidebars to wordpress themes"...

Gravatar

Commented by: Cherry Blossom Girl, 29 September, 2010

Was looking for a tutorial to add custom sidebars in Wordpress (dynamic ones, for the widgets). This tutorial is better than the one on WP.org itself.

Helped me a lot, thank you.

Gravatar

Commented by: Php Testing, 5 October, 2010

Nice post . keep adding more about your post.

Gravatar

Commented by: Mike, 21 November, 2010

Here's a similar twist using a plugin I just wrote. <a href=http://www.mlynn.org/graceful-sidebar-plugin>The graceful sidebar widget plugin</a> creates a widget that enables you to use custom fields in a post or page that display in the sidebar. You can use it to display custom html or a simple message. The custom fields it uses are graceful_title and graceful_content.

Enjoy!

Gravatar

Commented by: lisa, 22 November, 2010

hi! thank you very much for this post!
but just wondering, how would i make a PAGE specific sidebar?
your help would be much appreciated!

Gravatar

Commented by: foxhats, 26 November, 2010

I have been able to successfully add the sidebar to individual posts but it is not in the format of my theme,Thank you for sharing

New Era Hats

Gravatar

Commented by: joon, 27 November, 2010

Thanks very much for this. I’m always amazed how much of a difference it makes to have photos included with a post. I guess we’re all visual creatures!

Gravatar

Commented by: prefabrik, 16 December, 2010

Great tutorial. Definitely educational for us.

  • 2011-11-18

    Have You Secured Your Blog?

    Operating your own blog is not only fun and entertaining, but it can also turn into a profitable venture. Many beginner bloggers are under the impression that they can simply open an account with one of the popu...
    security problem
  • young bloggers
    2011-05-24

    Top 5 Blogging Platforms 2011

    What are the key ingredients of an excellent blogging platform? Prominence, high average readership, a clean visual presentation, and the ability to adapt to changing trends are vital components to any platform ...
  • wordpress beginners3
    2011-03-09

    The Beginner's Guide to WordPress

    Launching a successful blog takes time. But thankfully, simple blogging scripts like WordPress have made the start-up process much easier. Even if you have minimal computer skills, you can build your own blog us...
  • wordpressthemes2
    2010-12-08

    The Best WordPress Themes for Small Businesses

    When starting a small business it’s important to make sure potential customers see your product, whether it’s a service, portfolio or online store. WordPress offers unlimited options to showcase your busines...
  • 2010-09-03

    Why Start a Blog?

    The online world of blogging, communicating your thoughts, ideas, and opinions for all the world to see, can be an intimidating community to consider joining. Why start a blog in the first place, and what possib...
  • 2010-09-03

    Starting a Blog – The First Steps

    Did you know that our current culture is one of the most literary leaning in the history of civilization? Not since ancient times have the general populous been engaged in keeping diaries of their daily lives. W...
  • 2009-11-05

    20 Great UX Blogs for Web Designers

    Usability and user experience (UX) are often underestimated by web designers but if you want your sites to have a professional touch as well as keeping your vistors happy, you can't afford not to pay attention t...
  • 2009-04-28

    21 Wordpress Plugins Of All Time

    As there are some +4000 plugins out there we thought it is well about time to create a more comprehensible list for you fellow wp-peers. We spent last weekend testing Worpdress plugins, around the clock until...
  • 2009-03-26

    Six Easy Steps DIY: Move Your Wordpress.com Blog

    Wordpress.com currently hosts millions of blogs (and counting), most of them content with WordPress as the hosting provider. But every year thousands of bloggers still make the journey to the other side - the...
  • 2009-03-03

    Stop Tweaking! Create Your Own Wordpress Theme

    This tutorial goes out to anybody who has ever found a Wordpress theme he thought he liked only to end up tweaking the stylesheet and template files until it looked nothing like the original. We ask you this;...

1 2

Overall Best Web Hosts

1. $2.95Review Visit
2. $4.45Review Visit
3. $3.67Review Visit
4. $4.95Review Visit
5. $5.95Review Visit

Buying Guide

Can't decide what hosting is right for you? Answer 2-5 questions and get your perfect hosting match!

To Questions

User Reviews

Make your voice heard. Review your web hosting provider - good or bad.

  •  
  •  
  •  
  •  
Everything has been very stabile and I was very impressed with all the features and extras that were included in the plan.

Bill about iPage

Read iPage Review