WordPress, Multisite and BuddyPress plugins, themes and support


You’ve found the perfect theme for your WordPress installation. It already has most of what you need, except you’d like to include a different sidebar on some of the pages. Or perhaps you would like to have a certain sidebar included based on the category of posts you are displaying. Now there’s an easy way to do this using custom fields. Much thanks to Jean-Baptiste Jung over at WP Recipes for his helpful post. Changing the sidebar based on a custom field is especially helpful if you’re a developer delivering multiple templates to accommodate a client who wants to make his own updates. Creating content with a more modular layout is much simpler when using custom fields.

Here’s the How To:

1. Open single.php* and find the call to the function:

<?php

get_sidebar();
?>

2. Replace that with:

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

get_sidebar($sidebar);
?>

3. Whenever you create a new post, you will be able to include the alternate sidebar by creating a custom field called “sidebar” with a value of “2”. WordPress will then automatically include sidebar-2.php instead of the default sidebar. So easy!

*You can also do this on page.php.

You can even have multiples for header.php and footer.php by using the following:

For header:

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

get_header($header);
?>

For footer:

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

get_footer($footer);
?>

The idea is to use your space more efficiently and to target your information to the right page or post, instead of having to settle with one default sidebar/header/footer. This makes your site more interesting for the users as well as an opportunity to include more rich content which will boost your SEO. Creating a more modular layout also enables you to build pages that are more CMS oriented with less of a blog style feel.

Innovative Uses for Multiple Sidebars/ Headers/ Footers Include:

1. Create more space for advertising
2. Display more pertinent content
3. Customize user profile templates in community sites based on user type.
4. Feature more users, groups, discussion topics, based on the selected category
5. Get niche specific for your users with selected RSS feeds or API’s from social networks
6. Drive traffic to key pages, categories, or posts
7. Include sign-up forms on certain page headers or sidebars
8. Generate and customize user specific headers for password protected posts or pages

The possibilities are endless! Subscribe now for more tips on bending your WordPress installation to your needs.

Post to Twitter Tweet This Post