• Resolved alingham

    (@alingham)


    Hi all,
    I hope someone out there will be able to help.

    Basically, I’ve been scouring the net all day today trying to find a way that will make this work for me. I’ve read every post there is on selective refresh, partials, javascript api and customizer, and have yet to find a decent tutorial that goes through and explains exactly what to do when it comes to the following:

    I want the user to be able to select how many “pages” appear on my one page theme.

    The way I have it set up is with a text box control as “number-of-pages“.
    Depending on the number entered into that control (i.e. 5) is the number of times I then loop to create “5” dropdown-pages controls.

    //Panel
     $wp_customize->add_panel( 'strip_option', array(
     'priority' => 100,
     'capability' => 'edit_theme_options',
     'title' => __('Strip Theme Options', strip_title),
     'description' => __('Strip theme options to make your site awesome.', strip_title),
     ));
    
     
     //====================================Section for Homepage Pages====================================//
     $wp_customize->add_section('strip_pages', array(
     'priority' => 5,
     'title' => __('Homepage Pages', strip_title),
     'panel' => 'strip_option',
     ));
     //====================================Settings for Homepage Pages====================================//
     // Number of Pages
     $wp_customize->add_setting( 'strip-number-of-pages', array(
    			'default'           => '',
    			'sanitize_callback' => 'absint',
    			'transport' => 'refresh',
      ) );
      $wp_customize->add_control( 'strip-number-of-pages', array(
    			'label'    => __( 'Number of Pages', 'textdomain' ),
    			'section'  => 'strip_pages',
    			'type'     => 'text',
    			'placeholder' => '5',
      ) );
    
    // NOTE: HERE IS WHERE I AM TRYING TO REFRESH THE VARIABLE BEFORE THE FOR LOOP IS RUN...
      $wp_customize->get_setting( 'strip-number-of-pages' )->transport = 'refresh';
      $wp_customize->selective_refresh->add_partial( 'strip-number-of-pages', array(
        'selector' => '.strip-number-of-pages',
        'render_callback' => '__return_false',
    ) );
    // RESUME rest of code....
      $num = get_theme_mod( 'strip-number-of-pages' );
      
    // Page Selects
       for ($count=1; $count<=$num; $count++) {
    		// Page Select
    		$wp_customize->add_setting( 'strip-page-' . $count, array(
    			'default'           => '',
    			'sanitize_callback' => 'absint'
    		) );
    		
    		if($count == 1) {
    			$wp_customize->add_control( 'strip-page-' . $count, array(
    				'label'    => __( 'Select Home Page', 'textdomain' ),
    				'section'  => 'strip_pages',
    				'type'     => 'dropdown-pages',
    			) );
    		} else {
    			$wp_customize->add_control( 'strip-page-' . $count, array(
    				'label'    => __( 'Select Page ' . $count , 'textdomain' ),
    				'section'  => 'strip_pages',
    				'type'     => 'dropdown-pages',
    			) );
    			
    		} // end if/else
    	}//end of for
    

    The issue I am running into is that when I enter a number into “number-of-pages“, the “for loop” has already run, and so it will be displaying whatever “number-of-pages” was set previously. At the moment, to solve this, I have to refresh the entire page after clicking “Publish” and then the “for loop” runs again with the newly saved “number-of-pages“.

    Ultimately, what I would like to happen is when the user types in a new “number-of-pages“, the for loop is run again with the new value…
    Alternatively, the next best option would be that when the user types in a new “number-of-pages“, they click on “Publish” and the for loop runs again with the new value…
    At the moment the user has to type in a new “number-of-pages“, click Publish, and then refresh the entire page in order to make the for loop run again with the new value, and this makes it somewhat unusable…

    TIA.

    • This topic was modified 7 years, 3 months ago by alingham.
    • This topic was modified 7 years, 3 months ago by alingham. Reason: formatting
Viewing 3 replies - 1 through 3 (of 3 total)
  • Yes, you have hit the fact that the PHP part of the Customizer pane runs only once. If you want something to change after it’s loaded, you have to use javascript.
    The Twenty Seventeen theme has optional Page sections added into the home page. You can look at how that was done. But a lot of the dynamic controls are created in javascript. See Menu Items and Widgets.

    Thread Starter alingham

    (@alingham)

    I’ve had a look, but no idea what I’m actually looking for.
    None of it makes sense or even looks close to anything I need.

    Think again how to structure your option. Why store the number? Why not just have a button that says “Add”, and when it is clicked, you add another select control for choosing a page. The list of pages goes into one option that is an array of IDs. Only a little part of this is PHP. The rest is javascript.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Customizer – How to refresh a variable in For Loop’ is closed to new replies.