Friday, March 25, 2011

Different Sidebar for wordpress pages

Often one needs different sidebars for different pages in wordpress. This can be implemented using the below mentioned code.The logic used here is that we get the post id and assign sidebar based on their id's.
<div id="sidebar">

<?php
global $id;
$post_id_current = get_post($id,ARRAY_A);
$post_parent = $post_id_current['post_parent'];
if($post_parent!='')
{
$my_id=$post_parent;
}
else
{
$my_id=$id;
}

if($my_id!==4 )
{
 if ( !function_exists('dynamic_sidebar')

|| !dynamic_sidebar('sidebar2') ) : endif;

}
else
{
 if ( !function_exists('dynamic_sidebar')

|| !dynamic_sidebar('sidebar1') ) :  endif;
}
?>
</div>

2 comments:

  1. Seems good info about wordpress only, If u have expert in wordpress, can u more post tutorial tips for new user. we are waiting for ur next post ---
    Thanks
    Sid

    ReplyDelete
  2. Hey buddy!
    Thanks Sidd
    You will see posts on magento , drupal and core php too in coming posts.It's just a beginning.

    ReplyDelete