Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i registered my sidebars as follows:

$sidebars = array (
            'sidebar-10'       => 'Hoof Sidebar',
            'sidebar-11'       => 'Tuisbladsy area een',
            'sidebar-12'       => 'Tuisbladsy area twee',
            'sidebar-13'       => 'Tuisbladsy area drie',
            'sidebar-14'       => 'Tuisbladsy area vier',
            'sidebar-15'       => 'Voet area een',
            'sidebar-16'       => 'Voet area twee',
            'sidebar-17'       => 'Voet area drie',
            'sidebar-18'       => 'Voet area vier',
            'sidebar-19'       => 'Onder artikel 1 area',
            'sidebar-20'       => 'Advertensie area onder die hoof 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>',
    ));
}

The thing is, "Hoof 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

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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>',
    ));
}
share|improve this answer
    
it works perfectly, many thanks andrew –  Pieter Goosen May 12 '13 at 16:48
    
Sure thing, happy coding. –  Andrew Bartel May 12 '13 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. –  Pieter Goosen May 15 '13 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. –  Andrew Bartel May 16 '13 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 –  Pieter Goosen May 18 '13 at 8:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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