My Latest Posts

living with passion

With all my professional experience training in homeopathic treatments and specialising in certain areas I hope you will find some of my writing interesting.

Test Article 2

Create Multiple WordPress Sidebars

with get_sidebar() – Ok so if you have been coding with WordPress for a while you might have felt the need to code multiple sidebars, this allows you to customize banner adverts and different widgets for different category landing pages. You will have noticed that the default method to add a sidebar using get_sidebar() function only allows for a single sidebar named sidebar.php – searching for the most effective way to do this I think I have discovered the most effective way to get this done.

This method works, but in the interest of keeping our code readable and simplifying the inclusion of multiple sidebars, there’s an easier way to do this and it’s incredibly simple.

include(‘TEMPLATEPATH . ‘/right-sidebar.php’;

This method works, but in the interest of keeping our code readable and simplifying the inclusion of multiple sidebars, there’s an easier way to do this and it’s incredibly simple.

Including sidebar-left.php and sidebar-right.php using get_sidebar

Let’s say we’ve got two sidebars, right and left, and we’ll name the files sidebar-right.php and sidebar-left.php respectively. To include those files, all we have to do is use the get_sidebar function but add parameters to each function call to tell WordPress which sidebar we want included where.

To include the file “sidebar-right.php” we’ll use:

get_sidebar(‘right’);

And to include our other sidebar, “sidebar-left.php” we’ll use:

get_sidebar(‘left’);

Likewise, if we want to include a third sidebar, sidebar-right-2.php for example, we’ll use:

get_sidebar(‘right-2’);

Syntax of get_sidebar()

It’s that simple. The key to remember here is that, as commentator Rakeshkumar Mehta points out, the get_sidebar function prepends “sidebar- to the argument you feed it, so all your sidebar files should start with “sidebar-“. For example, to include a file named sidebar-newsidebar.php, use get_sidebar(‘newsidebar’), or to include a file named sidebar-my_right_sidebar.php, use get_sidebar(‘my_right_sidebar’).

News Tags: ,