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.

Im trying to create like, 8 sidebar areas using an array of page templates (like date, archive, tag ect) and then looping through the array, creating a sidebar for each value.

I thought this should work, but it only creates a sidebar for the last value in the array:

$widget_areas = array('single','index','category','archive','tag','taxonomy','author','date','search','attachment');
foreach($widget_areas as $area){


    register_sidebar(
         array(
        'name' => __( $area. ' Widgets' ),
        'id' => 'index',
        'description' => __( 'Widgets in this area will appear on the ' . $area ),
        'before_widget' =>'<div class="widget-container">',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
        'after_widget' => '</div>'
    )
    );

}

The only sidebar it's creating is for the "attachment" template. What gives? Shouldnt register_sidebar run for each value in that array, creating a ton of sidebars? If I hardcode each section with register_sidebar that works, but I really dont want to repeat a bunch of code

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Pass a different id for each sidebar or you will overwrite the same sidebar again and again. You can also just leave the id argument out, and WordPress will create one automatically.

Side note:

__( $area. ' Widgets' )

and

__( 'Widgets in this area will appear on the ' . $area )

… are wrong. The translation function expects a second parameter textdomain, and you cannot use a variable in the string that should be translated.

Use sprintf() with a placeholder to pass the variable, and use a context function to explain the parameter:

sprintf( 
    _x( 
        '%s Widgets', 
        '%s = widget area name', 
        'your_theme_textdomain' 
    ), 
    $area
)
share|improve this answer
1  
I am not a smart, or careful person. I passed the $area variable to the description value but not the ID. Thanks for pointing that out and explaining the translation funtion –  rugbert Jul 28 '13 at 14:23
add comment

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.