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 Tutorials,
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...
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 <a href="http://codex.wordpress.org/WordPress_Widgets">Sidebar Widgets</a> 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.<span id="more-1349"></span> <h3>a) Register a sidebar</h3> <h3>b) Create the dynamic sidebar location</h3> this usually will be in your WordPress theme sidebar.php Now in <em>wp-admin->appereance->widgets</em>, you will see a 'Sidebar' widget area in right hand side. <strong class="headliner">But what if you want to have dynamic sidebar that change if you're on WordPress pages or single post?</strong> 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. <h3>a) Register a sidebar, page sidebar and single sidebar</h3> <h3>b) Add this to top of sidebar.php </h3> <h3>c) Use this code to call the dynamic sidebar location</h3> 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.

You might also like to read:

WordPress Tutorials

How to Remove Images from the_content() but Preserve HTML Formating

WordPress Tutorials

Easier Retina Support for Your WordPress Themes

WordPress Tutorials

How to Show All Child Categories from Parent Category in WordPress

Like Our Posts? Subscribes Now!

Preview on Feedage: dezzaincom

Add to My Yahoo! Dezzain.com

Add to Google! Dezzain.com

Add to AOL! Dezzain.com

Add to MSN Dezzain.com

Subscribe in NewsGator Online Dezzain.com

Add to Netvibes Dezzain.com

Subscribe in Pakeflakes Dezzain.com

Add to Windows Live Dezzain.com

iPing-it Dezzain.com

Add To Fwicki Dezzain.com