Simplifying the code for displaying multiple widget areas in Genesis

Simplifying the code for displaying multiple widget areas in Genesis

4.9/5 - (371 votes)

Let’s say you want to display 5 widget areas for the front page. The code for this is something like

genesis_widget_area( 'front-page-1', array(
    'before' => '<div class="front-page-1 widget-area"><div class="wrap">',
    'after'  => '</div></div>',
) );

genesis_widget_area( 'front-page-2', array(
    'before' => '<div class="front-page-2 widget-area"><div class="wrap">',
    'after'  => '</div></div>',
) );

genesis_widget_area( 'front-page-3', array(
    'before' => '<div class="front-page-3 widget-area"><div class="wrap">',
    'after'  => '</div></div>',
) );

genesis_widget_area( 'front-page-4', array(
    'before' => '<div class="front-page-4 widget-area"><div class="wrap">',
    'after'  => '</div></div>',
) );

genesis_widget_area( 'front-page-5', array(
    'before' => '<div class="front-page-5 widget-area"><div class="wrap">',
    'after'  => '</div></div>',
) );

We can simply rewrite the above using a for loop like so:

// Display front-page widget areas.
for ( $i = 1; $i <= 5; $i++ ) {
    genesis_widget_area( "front-page-{$i}", array(
        'before' => '<div class="front-page-' . $i . ' widget-area"><div class="wrap">',
        'after'  => '</div></div>',
    ) );
}