Register multiple sidebars

I am registering my sidebars as follows:

$sidebars = array (
            'sidebar-10'       => 'Main Sidebar',
            'sidebar-11'       => 'Homepage Area One',
            'sidebar-12'       => 'Homepage Area Two',
            'sidebar-13'       => 'Homepage Area Three',
            'sidebar-14'       => 'Homepage Area Four',
            'sidebar-15'       => 'Footer Area One',
            'sidebar-16'       => 'Footer Area Two',
            'sidebar-17'       => 'Footer Area Three',
            'sidebar-18'       => 'Footer Area Four',
            'sidebar-19'       => 'After first post area',
            'sidebar-20'       => 'Below header area',
            );
foreach ( $sidebars as $sidebar ) {
register_sidebar(
    array (
            'name'          => __( $sidebar, 'pietergoosen' ),
            'id'            => $sidebar,
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget'  => '</aside>',
            'before_title'  => '<h3 class="widget-title">',
            'after_title'   => '</h3>',
    ));
}

Main Sidebar is the name of my widget. I need to set sidebar-10 as the widget id. The same with the rest as in my array.

Any suggestions solving this

1 Answer
1

You just need to use the alternate syntax for foreach. From the php manual:

The foreach construct provides an easy way to iterate over arrays.
foreach works only on arrays and objects, and will issue an error when
you try to use it on a variable with a different data type or an
uninitialized variable. There are two syntaxes:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

For your example:

foreach ( $sidebars as $id => $sidebar) {
register_sidebar(
    array (
            'name'          => __( $sidebar, 'pietergoosen' ),
            'id'            => $id,
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget'  => '</aside>',
            'before_title'  => '<h3 class="widget-title">',
            'after_title'   => '</h3>',
    ));
}

Leave a Comment