Do you ever wanted to be able to load different sidebars according to the current category? Here is a very simple recipe to learn how to easily create a dynamic sidebar.
Do you ever wanted to be able to load different sidebars according to the current category? Here is a very simple recipe to learn how to easily create a dynamic sidebar.
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
}
?>
Credits goes to Chris Cagle for this awesome recipe!
Leave a Comment