Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

106 PART II • Designing and Developing WordPress Themes


'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
) );

// Sidebar in the header
register_sidebar( array(
'name' => 'Header widget area',
'id' => 'header-sidebar',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
) );

// Sidebar in the footer
register_sidebar( array(
'name' => 'Footer widget area',
'id' => 'footer-sidebar',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
) );
}

Speaks for itself, right? This is basically the same declaration as for one (default) widget area
but with the names of every area defined. This will have to carry on to the code that defines
the actual areas in the template files. That doesn’t require any fancy footwork; just add the
widget area ID (preferably) to the first PHP tag for defining an area, like this:

<?php dynamic_sidebar( 'the-widget-area-ID' ); ?>

So the footer area would look like this:

<?php dynamic_sidebar( 'footer-sidebar' ); ?>

Simple and straightforward. Naturally, anything that goes for single widget areas can be used when
you have multiple widget areas, so if you need them to behave differently, by all means go for it.

CUSTOMIZING WIDGETS
Sometimes you may not want the widgets to output the way they do by default. Maybe you
want to enclose them in div tags, for example. To do so, register them in functions.php using
an array:

<?php
register_sidebar( array(
'before_widget' => '',
Free download pdf