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:

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

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

Your choice for site templates and wordpress themes


Got something to say?

You must be logged in to post a comment.