WordPress: Include a Different Sidebar for Certain Pages

Here’s how to include different sidebars for particular pages:


<?php
if(is_page()) {
 // We're on a "page", which?
 if (is_page('About')) {
 // Ah, the about page!
 get_sidebar('about'); // includes sidebar-about.php
 } elseif (is_page('archives')) {
 get_sidebar('archives'); // includes sidebar-archives.php
 } elseif (is_page('contact')) {
 get_sidebar('contact'); // includes sidebar-contact.php
 } else {
 // if we're not any of the above pages
 get_sidebar(); // includes regular sidebar.php
} else {
 get_sidebar(); // includes regular sidebar.php
}
?>

If you want to extend it further, such as displaying different sidebars for different categories, use this code:

< ?php
if(is_page()) {
    // We're on a "page", which?
     if (is_page('About')) {
        // Ah, the about page!
          get_sidebar('about'); // includes sidebar-about.php
    } elseif (is_page('archives')) {
          get_sidebar('archives'); // includes sidebar-archives.php
    } elseif (is_page('contact')) {
          get_sidebar('contact'); // includes sidebar-contact.php
    } else {
        // if we're not any of the above pages
          get_sidebar(); // includes regular sidebar.php
} else {
    get_sidebar(); // includes regular sidebar.php
}
?>

Related posts:

  1. Formatting Archive Page Titles in WordPress
  2. How to Build Facebook Pages
  3. WordPress Landing Pages
  4. How to Add Multiple Sidebars to WordPress
  5. Duplicating Pages/Posts Quickly in WordPress

2 Comments

Got something to say? Feel free, I want to hear from you! Leave a Comment

  1. Mike says:

    Or for a code-less approach you could install my Graceful Sidebar Plugin. This plugin enables you to create custom sidebar content for each page or post using custom fields.

    Enjoy!

  2. Jean Galea says:

    Thanks Mike, nice solution :)

Leave a Comment

Let us know your thoughts on this post but remember to place nicely folks!