Most all themes will have a sidebar file. This will provide the sidebar that usually controls where the widgets will go.
Most themes have a single sidebar. Depending upon your theme, you may find your side bar to the left or the right of the main content.
However, some themes have additional columns. These themes may have multiple sidebar files, so always check your themes.
Let’s look at a basic sidebar.php file.
Samples Sidebar file:
HTML
aside
The <aside>
tag. This tag is used to display information to the side of the main content, and is specific to HTML 5. This means this theme may not work correctly in older browsers, but at this point in time, most people don’t need to realistically worry about supporting IE 6 and 7.
Inside the <aside>
is a div
tag that can be used for styling and scripting. In this case, it has not classes associated with it, although you may for your theme. It does have an ID, so you can style via that.
Unordered lists
The widgets will then be put into a unordered list. This is fairly common, and is easily styled against with CSS.
PHP
if:/endif
The main things to look at in the sidebar is the if
statement. This is checking to see if there is anything in the sidebar that has been given a name. In some themes, if there is nothing in the sidebar widget area, they will place their own default content. This will use the else clause of the if statement in PHP.
Other themes, like the one seen here, will leave the area blank if it does not have any active widgets in the area.
It is common in themes to use the if block in this manner (if: <statements> endif;) You may also see if { } as a methodology as well. The behave the same way, and is merely a matter of preference.
is_active_sidebar
is_active_sidebar()
is a WordPress function. It checks to see if there are any widgets in a sidebar area. These widgets would be placed using the admin widgets page. It returns a Boolean value, so it works well with a if statement.
dynamic_sidebar
dynamic_sidebar
is a WordPress function that tells WordPress to load a sidebar widget area. This is known as a sidebar, even though you have have widget areas in places other than the sidebar. It will find the widget area with that name, and insert those widgets, as HTML, into your page.
But you might wonder, how does that widget area get defined. What that is in another file, called the functions.php.
functions.php
Most every theme will have a functions.php. This file is used to handle a variety of tasks associated by the theme. One such task is registering widget areas.
Functions.php has another interesting feature. If, in a child theme, you have a file that has the same name as what exists in the parent, the parent version get overwritten. The exception is the functions.php file. Both files are allowed to work together.
Your completed functions.php (for the relevant area) is shown below.
// add the theme_widgets_init to the widgets_init hook add_action( 'widgets_init', 'theme_widgets_init' ); // our custom theme to register the hooks. function theme_widgets_init() { register_sidebar( array ( 'name' => __( 'Sidebar Widget Area' ), 'id' => 'primary-widget-area', 'before_title' => '', 'after_title' => '
', ) ); }
add_action()
WordPress uses a series of “hooks” that you can attach actions to. This is how plugins work, and know how and when to run.
To attach your code to one of those hooks, you will use the add_action
function. It takes two parameters. The first is which hook you are attaching to. The second parameter is the function name you’ve created, that will be called when WordPress calls the function(s) associated with that hook.
One of those hooks is widgets_init. This is when WordPress wants to initialize the widget area(s).
register_sidebar()
register_sidebar
is a special function which defines not only the sidebar, but also provides information about how it will be displayed. You pass a single parameter to the function, but that is an array of values. It is an array, because there are a lot of potential values that you can set, and WordPress didn’t want you to have to set all of them, which you would have if is was individual parameters.
You must give your widget area a name (as displayed to admins) and an id. Ids must be unique, and should not have spaces. You can also define a description for administrators if you want, but that is optional.
You also have before_title and after_title. This is normally a HTML tag, such as ‘<h3>’, ‘</h3>’ respectively. You can also set widget before_widget
and after_widget parameters if you would like. Because these are optional values, they have default values.
You can find more information at http://codex.wordpress.org/Function_Reference/register_sidebar
Developing a Theme – Sidebar was originally found on Access 2 Learn