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){

        '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

1 Answer 1


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' )


__( '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:

        '%s Widgets', 
        '%s = widget area name', 
  • 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
    Commented Jul 28, 2013 at 14:23

Your Answer

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

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