4

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

9

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>',
    ));
}
5
  • Sure thing, happy coding. May 12, 2013 at 18:47
  • Andrew, is there a way of doing this if you have three variables? I scratched around in the php manual, but got no wiser. The thing is, I'm still a newby in php. As above, if I add the description of the sidebar to the array of the name and id like this " 'name' => 'id' => 'description' ", will it work, does php and more so, Wordpress, allow this. May 15, 2013 at 16:44
  • Foreach loops over a key => value pair. You can set the value to an array to store multiple values, like the description and the name if necessary by creating nested arrays when you originally build the $sidebars array. May 16, 2013 at 17:48
  • If it is possible please, can you give me an example of how to do it. I'm a bit new at this May 18, 2013 at 8:53
  • 1
    i got it working. Seems like I'm getting php coding. Here is my code. pastebin.com/83ikqdk6 May 19, 2013 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.