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

(avery) #1

CHAPTER 4 • WordPress Theme Essentials 105


Although this will work, the proper way to register a sidebar is by hooking on to widgets_
init, which means that you need to write a function for register_sidebar() and then
pass that to the hook, like so:


add_action( 'widgets_init', 'smashing_register_sidebars' );


function smashing_register_sidebars() {
// Register sidebars
register_sidebar();
}


This will hook smashing_register_sidebars() to widgets_init, which in turn
means that register_sidebars() will be run.


Then add this to the part of sidebar.php where you want the widgets to show up. It should go
within the ul tags:



For a more detailed example of how sidebar.php can look with default content in it, see the
“The Right Column: sidebar.php” subsection of “Creating the Template Files” earlier in this
chapter.


USING MULTIPLE WIDGET AREAS


Some themes have more than one widget area. You can accomplish this by declaring the
widget areas in functions.php a little differently. If you want two sidebar areas, a header area,
and a footer area that are widget-ready, just add the following code to functions.php:


add_action( 'widgets_init', 'smashing_register_sidebars' );


function smashing_register_sidebars() {


// The first sidebar
register_sidebar( array(
'name' => 'First sidebar',
'id' => 'first-sidebar',
'before_widget' => '

  • ',
    'after_widget' => '
  • ',
    'before_title' => '

    ',
    'after_title' => '

    '
    ) );


    // The second sidebar
    register_sidebar( array(
    'name' => 'Second sidebar',
    'id' => 'second-sidebar',

    Free download pdf