Create a dynamic sidebar for wordpress

November 5, 2008 · Print This Article

Do you ever wanted to be able to load different sidebars according to the current category?

Since WordPress 2.5, you can specify a sidebar name to be inclued:

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

The above code will include the file named sidebar-name.php.

The following code will include a custom sidebar according to the category you are on:

<?php
//to be able to use this outside the loop
if ( have_posts() ) { the_post(); rewind_posts(); }

if ( in_category('1') ) {
get_sidebar('cat1');
//gets sidebar-cat1.php
} elseif ( in_category('2') ) {
get_sidebar('cat2');
//gets sidebar-cat2.php
} elseif ( in_category('3') ) {
get_sidebar('cat3');
//gets sidebar-cat3.php
} elseif ( in_category('4') || in_category('5') || in_category('6') ) {
get_sidebar('catRest');
//gets sidebar-catRest.php
} else {
get_sidebar()
//gets sidebar.php
}
?>


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.