Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to do a dynamic sidebar for my WordPress site, so it would pull in the correct Sidebar template based on the page title of the page.

So if I had 10 sidebars all named like this:

Sidebar Template Name: About Sidebar Template Name: Contact Sidebar Template Name: Jobs Sidebar Template Name: Etc Sidebar Template Name: Page1

So the Sidebar 'About' would appear on the About page, and the same for contact, etc...

Any idea How I would do this? Or Is there an alternative way on how to do it?

share|improve this question
add comment

3 Answers

Something like this should work:

if( is_page() ) :
    $this_page = get_queried_object();
    if ( ! dynamic_sidebar( $this_page->post_title ) ) :
        echo 'No sidebar defined for this page title';
    endif;
endif;
share|improve this answer
add comment

you can try to include different sidebars when creating new template

name a sidebar like sidebar-jobs.php then when creating template name: Jobs, just call this sidebar like so

<?php get_sidebar('jobs'); ?>

create metabox and list all registered sidebars

<select>
  <?php foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) { ?>
 <option value="<?php echo ucwords( $sidebar['id'] ); ?>">
          <?php echo ucwords( $sidebar['name'] ); ?>
 </option>

then based on selected value - call selected sidebar

share|improve this answer
    
But then I will need a separate template for every separate sidebar page. I have 1 template, multiple pages which will have multiple sidebar templates. –  Owen O'Neill Sep 6 '12 at 17:54
    
then you can create array, I updated my answer above –  alexndm Sep 6 '12 at 18:01
    
will this just give me a selector to select a sidebar for that page? If so this is spot on! –  Owen O'Neill Sep 6 '12 at 18:18
    
yes. it will. you need to make metabox for your page.php and place this <select> and that's it. I would recommend this metabox framework github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress I used it for displaying taxonomies and all pages inside metabox... works very well –  alexndm Sep 6 '12 at 18:20
add comment

Here is a live example from one of my sites:

if(is_page('about-us')) {
    dynamic_sidebar('main-sidebar');
}
if(is_page('contribute')) {
    dynamic_sidebar('page-sidebar');
}
if(is_front_page() && is_page('about-us')) {
    dynamic_sidebar('main-sidebar');
}
if(!is_front_page() && is_page('about-us')) {
    dynamic_sidebar('another-sidebar');
}

if(is_category('archive')) {
    dynamic_sidebar('archive-sidebar');
}

You can see various scenarios where I need different widgets.

Also, in functions.php, I have this code:

if(function_exists('register_sidebar')) {
    register_sidebar(array('name' => 'Homepage Sidebar'));
    register_sidebar(array('name' => 'Blog Sidebar'));

    register_sidebar(array(
        'name' => __('Main Sidebar'),
        'id' => 'main-sidebar',
        'description' => 'This sidebar is active on Homepage, Despre Noi, Directioneaza 2%, and individual profiles.'
    ));
    register_sidebar(array(
        'name' => __('Archive Sidebar'),
        'id' => 'archive-sidebar',
        'description' => 'This sidebar is active on Archive page.'
    ));
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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