I've created a sidebar called "Mountain". I can add it through the Wordpress back-end except for on the Taxonomy page. There I wanted to hard code it by replacing:

<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('post-widgets')) : else : ?>
<?php endif;?>


<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Mountain')) : else : ?>
<?php endif; ?>


<?php get_sidebar("Mountain");?>

Not sure why, but this is leaving the old sidebar in place and adding the "Mountain" sidebar at the bottom of my content.


First, let's make sure we're clear regarding terminology; there are two possible meanings for "sidebar" in your question:

  1. A dynamic sidebar, i.e. a Widget area, created via call to register_sidebar() in functions.php, and included in the template via call to dynamic_sidebar().
  2. A template part, called via get_sidebar( $slug ), the markup of which is defined in a template-part file named sidebar-$slug.php.

The two can, but do not have to, be related. For instance, calls to dynamic_sidebar() can, but do not have, to reside within a sidebar-$slug.php template-part file.

Let's step through both scenarios:

Dynamic Sidebar "Mountain"

So, assuming that your "Mountain" sidebar is a Widget area, you'll have something like the following defined in functions.php:

function wpse46048_register_dynamic_sidebars() {
    // Register "Mountain" Widget area
    register_sidebar( array( 
        'name' => 'Mountain',
        'id' => 'mountain',
        'description' => 'Mountain Widget area',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<div class="title widgettitle">',
        'after_title' => '</div>',
    ) );
add_action( 'widgets_init', 'wpse46048_register_dynamic_sidebars' );

...and then, somewhere in your template, you will call either this:

if ( ! dynamic_sidebar( 'mountain' ) ) :
    // Default content output goes here

...or else just this (if you don't want to output any default content):

<?php dynamic_sidebar( 'mountain' ); ?>

Then, you would populate the Widgets for this area via Dashboard -> Appearance -> Widgets, by dragging-and-dropping Widgets into the "Mountain" Widget area.

Template-part Sidebar "Mountain"

Assuming that your "Mountain" sidebar is a template part, you'll first define the markup for that template part in a file called sidebar-mountain.php. Then, you'll include that template-part file in the template via:

<?php get_sidebar( 'mountain' ); ?>
  • I was indeed talking about the dynamic sidebar, as described in your first example. I'm following exactly those steps and still, it's outputting the "default" sidebar on the page, and the "mountain" sidebar at the bottom of the content area. – WouterB Mar 19 '12 at 15:05
  • 2
    Well then, to answer more accurately, we'll need to see (full) code for the template files in question, to have an idea of what's going on. – Chip Bennett Mar 19 '12 at 15:49
  • Small side note that might not be obvious. This code <?php if( ! dynamic_sibar('mountain')): //default output endif; ?> actually prints the contents of the sidebar if has content. Very elegant though! – toni_lehtimaki Oct 7 '15 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.