Creating a Dynamic Sidebar with Conditional Tag for Your Sidebars

Sometime when dealing with dynamic content or sidebar, you need to change how or what the content or sidebar will show when visiting a certain pages or posts.

WordPress Sidebar Widgets allowed you to achieve this with the core dynamic sidebar. The common code for adding sidebar widgets into your WordPress theme will be like this.

a) Register a sidebar

function mytheme_widgets_init() {
   register_sidebar(array(
    'name'=>__('Sidebar', TEMPLATE_DOMAIN),
   	'id' => 'sidebar',
	'description' => __( 'Sidebar widget area', TEMPLATE_DOMAIN ),
	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	'after_widget' => '</aside>',
	'before_title' => '<h3 class="widgettitle">',
	'after_title' => '</h3>',
	));
}
add_action( 'widgets_init', 'mytheme_widgets_init' );

b) Create the dynamic sidebar location

this usually will be in your WordPress theme sidebar.php

<?php if ( is_active_sidebar( 'sidebar' ) ) : ?>
<?php dynamic_sidebar( 'sidebar' ); ?>
<?php endif; ?>

Now in wp-admin->appereance->widgets, you will see a ‘Sidebar’ widget area in right hand side.
But what if you want to have dynamic sidebar that change if you’re on WordPress pages or single post?

In this tutorial, i will show how to create dynamic sidebar with conditional tag for pages and single post. First you need to register a sidebar for pages and single post.

a) Register a sidebar, page sidebar and single sidebar

function mytheme_widgets_init() {
   register_sidebar(array(
    'name'=>__('Sidebar', TEMPLATE_DOMAIN),
   	'id' => 'sidebar',
	'description' => __( 'Sidebar widget area', TEMPLATE_DOMAIN ),
	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	'after_widget' => '</aside>',
	'before_title' => '<h3 class="widgettitle">',
	'after_title' => '</h3>',
	));
  register_sidebar(array(
    'name'=>__('Sidebar for Pages', TEMPLATE_DOMAIN),
   	'id' => 'sidebar-page',
	'description' => __( 'Sidebar for Pages widget area', TEMPLATE_DOMAIN ),
	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	'after_widget' => '</aside>',
	'before_title' => '<h3 class="widgettitle">',
	'after_title' => '</h3>',
	));
  register_sidebar(array(
    'name'=>__('Sidebar for Single Post', TEMPLATE_DOMAIN),
   	'id' => 'sidebar-single',
	'description' => __( 'Sidebar for Single Post widget area', TEMPLATE_DOMAIN ),
	'before_widget' => '<aside id="%1$s" class="widget %2$s">',
	'after_widget' => '</aside>',
	'before_title' => '<h3 class="widgettitle">',
	'after_title' => '</h3>',
	));
}
add_action( 'widgets_init', 'mytheme_widgets_init' );

b) Add this to top of sidebar.php

<?php
if( is_page() ) {
$dynamic_sidebar = 'sidebar-page';
} elseif( is_single() ) {
$dynamic_sidebar = 'sidebar-single';
} else { //else default sidebar
$dynamic_sidebar = 'sidebar';
}
?>

c) Use this code to call the dynamic sidebar location

<?php if ( is_active_sidebar( $dynamic_sidebar ) ) : ?>
<?php dynamic_sidebar( $dynamic_sidebar ); ?>
<?php endif; ?>

that’s it, now when you’re inside WordPress Pages, the dynamic sidebar will use ‘sidebar-page’ widget and when in single post, it will use the ‘sidebar-single’ widget.

Creating a Dynamic Sidebar with Conditional Tag for Your Sidebarshttp://cdn.dezzain.com/1/2013/09/dynamic-sidebars.jpghttp://cdn.dezzain.com/1/2013/09/dynamic-sidebars-300x300.jpg Richie KS WordPress Tutorialswordpress coding,WordPress Tips
Learn how to Create a Dynamic Sidebar with Conditional Tag for Your Sidebars so all your WordPress Pages will have different sidebars to show more alternative content.

8 Comments Already

  1. I find this tip long time ago, but not found widget have dynamic sidebar until found this article. Thanks for sharing.

    Public CommentUserI find this tip long time ago, but not found widget have dynamic sidebar until found this article. Thanks for sharing.
  2. I have to say thank you! I have been searching around forever trying to get my conditional dynamic sidebars to function properly and after reading this it made perfect sense…thank bro!

    Public CommentUserI have to say thank you! I have been searching around forever trying to get my conditional dynamic sidebars to function properly and after reading this it made perfect sense...thank bro!
  3. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is important and all. However imagine if you added
    some great images or video clips to give your
    posts more, “pop”! Your content is excellent but with pics and videos,
    this blog could certainly be one of the most beneficial in its niche.

    Good blog!

    Public CommentUserHave you ever thought about including a little bit more than just your articles? I mean, what you say is important and all. However imagine if you added some great images or video clips to give your posts more, "pop"! Your content is excellent but with pics and videos, this blog could certainly be one of the most beneficial in its niche. Good blog!
  4. can i get previous theme version link, its request ..

    Public CommentUsercan i get previous theme version link, its request ..
  5. Good tutorial,Richie. Thanks for sharing.

    Public CommentUserGood tutorial,Richie. Thanks for sharing.
  6. Damn it.. I am going crazy.. Sleepy may be.. This question was for Mesocolumn theme.. Kindly delete this comment and the previous one as well.. Sorry to bother you ofter..

    Regards,
    Nitin

    Public CommentUserDamn it.. I am going crazy.. Sleepy may be.. This question was for Mesocolumn theme.. Kindly delete this comment and the previous one as well.. Sorry to bother you ofter.. Regards, Nitin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>