Additional Widget Areas

In my theme, the sidebar is filled populated by using widgets. It would make sense for the links there to change depending on the type of content in the main window. If sermons are shown, then links for speakers, themes and sermon archives should be displayed. If it is newsletters, then categories and newsletter archives should be shown. For pages, a list of all the pages should be displayed. This was accomplished by adding more widget areas to the theme, then displaying the relevant widget area depending on what the main content is.

Adding Widget Areas

First, remove the existing action to initialize the default twenty ten widgets. The twenty ten function will be called later.

remove_action('widgets_init', 'twentyten_widgets_init');

Write a new function to call the default twenty ten widget function, and add the extra code for the additional widget areas.

function mbpc_widgets_init() {
	twentyten_widgets_init();

	// Area 3, will show up when certain conditions are met, see sidebar.php 
	register_sidebar( array(
		'name' => __( 'Third Widget Area', 'twentyten' ),
		'id' => 'third-widget-area',
		'description' => __( 'The third widget area', 'twentyten' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>>,
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

	// Area 4, will show up when certain conditions are met, see sidebar.php 
	register_sidebar( array(
		'name' => __( 'Fourth Widget Area', 'twentyten' ),
		'id' => 'fourth-widget-area',
		'description' => __( 'The fourth widget area', 'twentyten' ),
		'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
		'after_widget' => '</li>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	) );

}

Hook the function onto the widgets_init action.

add_action('widgets_init', 'mbpc_widgets_init');

There will now be additional widget areas which can be customized from the admin interface.

Loading Sidebars

This is relatively easy. WordPress already has the functions to check if a sidebar is active and to include it in a theme. In fact, it’s all there in the existing sidebar.php code. Use the is_active_sidebar('widget-name'); and dynamic_sidebar('widget-name'); functions.

Conditional Widgets

At each point in the code where a widget is loaded, there is a conditional check. To check for custom post types, use is_post_type_archive('cpt-name');. To check if it’s a custom taxonomy, is_tax(). For page templates, is_page_template('template-name.php');

By modifying the conditions to check for categories, pages, etc, I could choose a widget to display depending on the main content.

Add the Widgets

Now go into the admin interface and setup the widgets!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s