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.

How can I create a different side bar for single posts than for main page?

At the moment I have 2 working one for pages and one for the blog

thanks

this is how it looks in funtions.php

// Register Sidebars
register_sidebar(array('name' => 'Sidebar Blog','id' => 'sidebar-blog'));
register_sidebar(array('name' => 'Sidebar Pages','id' => 'sidebar-pages'));
register_sidebar(array('name' => 'Footer','id' => 'sidebar-footer'));

// Register Menus
register_nav_menu( 'top_navi', 'Top Navigation' );
register_nav_menu( 'main_navi', 'Main Navigation' );

this is how it looks in sidebar.php

<div id="sidebar">  
    <ul>

<?php
    if(is_page() && is_active_sidebar('sidebar-pages')) : dynamic_sidebar('sidebar-pages');
    elseif(is_active_sidebar('sidebar-blog')) : dynamic_sidebar('sidebar-blog');
else : ?>

    <?php wp_list_categories('title_li=<h2>Categories</h2>'); ?>

    <?php wp_list_pages('title_li=<h2>Pages</h2>'); ?>

    <li><h2>Archives</h2>
        <ul>
        <?php wp_get_archives(); ?>
        </ul>
    </li>

    <?php wp_list_bookmarks(); ?>

<?php endif; ?>

    </ul>
</div>

Thanks !

share|improve this question
add comment

2 Answers 2

I would consider the following ...

Register a new sidebar:

function your_new_widget() {
register_sidebar( array(
    'name'          => __( 'Single View Sidebar', 'your_textdomain' ),
    'id'            => 'sidebar-single',
    'description'   => __( 'This widget area is found only on the single post view.', 'your_textdomain' ),
) );
}
add_action( 'widgets_init', 'your_new_widget' );

Create a new sidebar template:

/** New file: sidebar-single.php */
if ( is_active_sidebar( 'sidebar-single' ) ) :
    dynamic_sidebar( 'sidebar-single' );
endif;

Then modify your 'single.php' template to call the new sidebar (above):

/** Modify single.php */
get_sidebar( 'single' );

Of course you will need to flesh out the new single view sidebar template file ... and the WordPress Template Hierarchy will take care of using your new sidebar on single post views.

share|improve this answer
    
This should work too. Also, it's good to include descriptions when you have multiple sidebars. However, creating a separate template page just for the sidebar is, I think, unnecessary unless there is a structural difference and not just a difference in widgets. –  Stephen Harris Jun 27 '12 at 12:29
add comment

Register the sidebars:

register_sidebar(array('name' => 'Sidebar Blog','id' => 'sidebar-blog'));
register_sidebar(array('name' => 'Sidebar Pages','id' => 'sidebar-pages'));
register_sidebar(array('name' => 'Sidebar Post','id' => 'sidebar-posts'));

Then, in your sidebar template, use the is_page() and is_single() conditional tags:

if( is_page() && is_active_sidebar('sidebar-pages') ){
     dynamic_sidebar('sidebar-pages');

}elseif( is_single() && is_active_sidebar('sidebar-posts') ){
     dynamic_sidebar('sidebar-posts');

}elseif( is_active_sidebar('sidebar-blog') ){
     dynamic_sidebar('sidebar-blog');

}else{
    //Default
}
share|improve this answer
    
it doesnt work ... thats what ive tried before ... any advise? –  qaedus Jun 26 '12 at 16:58
    
in what sense, doesn't work? What does it show instead - the sidebar-blog sidebar? Have you added widgets to the sidebar-posts sidebar? –  Stephen Harris Jun 26 '12 at 18:07
    
yes I did exactly what you said, but keeps showing the old sidebar in single posts too. –  qaedus Jun 26 '12 at 19:55
    
mmm... try var_dump(is_single()); at the top of sidebar.php. It should print 'true' on single posts and false otherwise. If it is not correct, something is interfering with the main query - likely a query_post() –  Stephen Harris Jun 26 '12 at 21:16
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.