X

Define a Specific Sidebar Using Custom Fields

Snippets by IsItWP

Do you want to define the custom sidebar for specific posts by replacing the get_sidebar() function? While there can be no plugin to achieve this, we have created a quick code snippet that you can use to define a specific sidebar using custom field in WordPress.

Instructions:

All you have to do is add this code to your theme’s template file where you want to call the custom sidebar or in a site-specific plugin:

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 62 best free WordPress blog themes and how to set up download tracking in WordPress with Google Analytics.

Comments  Leave a Reply

  1. thnks for the code

  2. teste

  3.  Hmm… not quite sure when I would use this one…

    1. I can see a few reasons to use this snippet if you have a site and you need to replace the sidebar on all but a few pages. Another could be displaying specific information for pages etc. It is a nice little snippet to keep in your hat on the off chance you need it. 🙂

      1. I read this… http://codex.wordpress.org/Function_Reference/get_sidebar
        Your post makes MUCH more sense to me now 🙂 

        1.  Ahh ok cool, I linked the document in the post so if others have questions they can view the wordpress codex.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!