Lego2012
12/14/2016 - 1:19 PM

Simplifying the code for displaying multiple widget areas in Genesis

Simplifying the code for displaying multiple widget areas in Genesis

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

<!-- 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"><div class="widget-area"><div class="wrap">',
    'after'  => '</div></div></div>',
) );
 
genesis_widget_area( 'front-page-2', array(
    'before' => '<div class="front-page-2"><div class="widget-area"><div class="wrap">',
    'after'  => '</div></div></div>',
) );
 
genesis_widget_area( 'front-page-3', array(
    'before' => '<div class="front-page-3"><div class="widget-area"><div class="wrap">',
    'after'  => '</div></div></div>',
) );
 
genesis_widget_area( 'front-page-4', array(
    'before' => '<div class="front-page-4"><div class="widget-area"><div class="wrap">',
    'after'  => '</div></div></div>',
) );
 
genesis_widget_area( 'front-page-5', array(
    'before' => '<div class="front-page-5"><div class="widget-area"><div class="wrap">',
    'after'  => '</div></div></div>',
) );

<!-- We can simply rewrite the above using a for loop like so: -->
for ( $i = 1; $i <= 5; $i++ ) {
    genesis_widget_area( "front-page-{$i}", array(
        'before' => '<div class="front-page-'.$i.'"><div class="widget-area"><div class="wrap">',
        'after'  => '</div></div></div>',
    ) );
}