Select Sidebar on a Post-by-Post Basis Using Custom Fields

Select Sidebar on a Post-by-Post Basis Using Custom Fields

If you’ve ever wanted to use different sidebars for different post categories or even individual posts, here’s a quick and easy way to put a little more customization into your templates.

Open your single.php file and locate the sidebar:

< ?php get_sidebar(); ?>

Replace it with this:

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

Now when you want to use a different sidebar, you’ll simply create a custom field called “sidebar.” Give it the value of the custom sidebar templates you’ve created- for example: sidebar-news.php, sidebar-full.php, sidebar-photos.php. If you want to use your news sidebar, give the custom field the name “sidebar” and the value “sidebar-news”.

FREE EBOOK
Your step-by-step roadmap to a profitable web dev business. From landing more clients to scaling like crazy.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

FREE EBOOK
Plan, build, and launch your next WP site without a hitch. Our checklist makes the process easy and repeatable.

By downloading this ebook I consent to occasionally receive emails from WPMU DEV.
We keep your email 100% private and do not spam.

Source: SpryeStudios.com

Tags: