0

I am trying to make a slider configurable via the customizer. In order to let the user decide how many sliders he wants, I added a setting where the user can enter a number.

Based on this number I want to generate sections, settings, and controls in the customizer, under my slider panel.

The function that is building the sections looks like this:

function createSliders($array) {
  foreach($array as $id) {
    $wp_customize->add_section( 
      'slider_'.$id, 
            array(
                'title'       => __( 'Slider '.$slider, 'Kraftzwerg' ),
                'capability'  => 'edit_theme_options',
                'panel'       => 'slider'
            ) 
        );
  $i++;
}

$array only holds the identifier (0-max) for the slider.

If I start the customizer I get an instant 500 error.

I know that I can't see the sections because there are no settings and controls for the section. But I didn't get over the 500 error.

Can someone tell me why it won't run?

0

I suppose you didn't pass $wp_customize properly. I'd do this:

  1. Create a setting e.x. section_number which determine user's choice
  2. Put the code to the customizer.php

customizer.php

add_action('customize_register', 'my_customizer');
function my_customizer($wp_customize){
   $section_number = get_theme_mod('section_number', 0);
   for($i = 0; $i <= $section_number; $i++) {
     $wp_customize->add_section( 
       'slider_'.$i, 
             array(
                 'title'       => __( 'Slider '.$slider, 'Kraftzwerg' ),
                 'capability'  => 'edit_theme_options',
                 'panel'       => 'slider'
             ) 
     );
}

Should work. Note that your sections won't appear if you won't attach any setting to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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