1

This works fine, but it's goofy to me as I don't know even what to search for (or if the question is titled correctly) to learn how to accomplish not repeating the $slide_panels array within the christina_slide_in_panels() function below it and outside it.

Sadly, this question was closed but the answer by Steve is in the comments. The question was not answered by reading the "duplicate" post accepted answer, at least not specifically.

//* register slide panels

$slide_panels = array (
            'slide-1'       => 'Slide Panel One',
            'slide-2'       => 'Slide Panel Two',
            'slide-3'       => 'Slide Panel Three',
            'slide-4'       => 'Slide Panel Four',
            );


foreach ( $slide_panels as $id => $slide_panel) {
    register_sidebar(
        array (
                'name'          => __( $slide_panel, 'christina' ),
                'id'            => $id,
                'before_widget' => '<aside id="%1$s" class="slide-widget %2$s">',
                'after_widget'  => '</aside>',
                'before_title'  => '<h3 class="slide-widget-title">',
                'after_title'   => '</h3>',
        ));

}


//* create the panels

function christina_slide_in_panels() {

   //======== > HOW TO NOT REPEAT THIS < ========
   $slide_panels = array (
            'slide-1'       => 'Slide Panel One',
            'slide-2'       => 'Slide Panel Two',
            'slide-3'       => 'Slide Panel Three',
            'slide-4'       => 'Slide Panel Four',
            );  

    echo '<!-- begin slide panels --><div class="slide-in-panels-parent">';

    foreach ( $slide_panels as $id => $slide_panel) {

        if ( is_active_sidebar( $id ) ) {

            echo '<div class="slide-panel" id="'.$id.'" aria-expanded="false" aria-hidden="true">';
            // id used on the toggle to get this slide in

            dynamic_sidebar( $id );

            echo '</div><!-- close '.$id.' -->';
        }
        //end endif;
    }
    //end foreach

    echo '</div><!-- end slide panels -->';
    //close parent;


}
add_action('genesis_after', 'christina_slide_in_panels');
Christina
  • 34,296
  • 17
  • 83
  • 119
  • Can you pass it in as a variable to the function when the function is called? – Scott Jan 28 '16 at 14:27
  • So do you only want to go through the foreach loop once? – Tom Withers Jan 28 '16 at 14:28
  • yes, it works exactly how I intend it to work, my question was trying to address not repeating the variable within a function and the answer I was pointed to is not dumbed down enough to me, but I will figure it out. – Christina Jan 28 '16 at 14:29
  • 1
    As the function and the array are in the same scope, you can make the function a closure that inherits the array: `$christina_slide_in_panels = function() use($slide_panels) {...}` and alter your add_action call: `add_action('genesis_after', $christina_slide_in_panels);` – Steve Jan 28 '16 at 14:39
  • Steve : thank you, I followed it and I get this error: syntax error, unexpected 'add_action' (T_STRING) on the closure for the foreach () BEFORE the $christina_slide_in_panels = function() use($slide_panels) – Christina Jan 28 '16 at 14:51
  • 1
    As a closure is a variable, you need to add a semicolon after the assignment: `$christina_slide_in_panels = function() use($slide_panels) {...};//<--here` – Steve Jan 28 '16 at 15:00
  • You are very, very kind. Thank you so much for not making my code look like a goof ball made it. – Christina Jan 28 '16 at 15:06

0 Answers0