If you have done even some of the most basic WordPress theme development, then you probably understand at least the basics of how theme templates work. Today I want to go a little bit beyond the basics and show you how to work with a really excellent, useful feature of the WordPress template system.
One of the most common aspects of WP themes is the use of “sidebars”, particularly widgetized sidebars, and most good themes have multiple configuration options for your sidebars: left, right, right wide, left wide, etc. Sometimes, using CSS is powerful enough for some of the layout options, but what if you want drastically different layouts, such as horizontal, short and narrow in the footer, below the post, etc? That’s where multiple sidebar.php template files come in handy.
By default, a theme’s sidebar is defined as what ever is in the file sidebar.php, and the contents of that file are then displayed with the function call
Well, let’s presume that we want to have three distinctly different sidebar layouts: one tall and narrow on the right (we’ll call this the default), one just below the post area in the main content section, and one in the footer, and all of them are widgetized. First what you need to do is create three files:
Next, you can use the same function I mentioned a moment ago to display the contents of the sidebars:
however, now, in order to pull in either the posts or the footer sidebar, we just need to add one parameter to the function:
1 2 3
get_sidebar('posts'); // or get_sidebar('footer');
Whatever the name is you placed after the “-” in the sidebar file name, is the parameter you will pass to the function.
Now you can easily have distinct “sidebars”, and as many as you want, that each have their own layouts, styles, widgets, etc. But remember, in order for your sidebar to be able to accept widgets, you have to enable it. If you don’t know how to do that, check out the codex entry on register_sidebar.