theme development - Add multiple sections, settings and controls at once to the Customizer

admin2025-01-07  6

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?

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?

Share Improve this question edited Oct 28, 2016 at 13:56 Pat J 12.3k2 gold badges28 silver badges36 bronze badges asked Oct 28, 2016 at 13:37 Martin MehlMartin Mehl 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262506a823.html

最新回复(0)