wordpress 特定于收音机选项的显示设置

ovfsdjhp  于 11个月前  发布在  WordPress
关注(0)|答案(2)|浏览(152)

我想创建一个条件,以便通过从收音机中选择一个选项,显示特定于该选项的设置。
我搜索了很多关于这个主题的信息,但没有找到任何有用的东西,所以我写了下面的简单代码。
这段代码几乎可以工作,也就是说,通过选择一个收音机选项并刷新整个屏幕,将显示所需选项的设置的实时预览。
是否有一种方法可以在选择收音机选项后立即显示该选项的设置,这样我就不必在每次选择收音机选项时刷新完整的实时预览屏幕?
我在functions.php文件中放入的代码:

function customize_pro($wp_customize){

$wp_customize->add_section('Select_txt_radio', array(
    'title' => esc_html__('Show or hide settings', 'section'),
    'priority' => 5,
 ));  
    

// Create a radio controller
    
$wp_customize->add_setting( 'setting_radio_main', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
 ) 
);

    
$wp_customize->add_control('setting_radio_main', 
array(
    
    'type' => 'radio',
    'label' => esc_html__('Available options'),
    'description' => esc_html__('By selecting each option, the corresponding settings will appear'),
    'section' => 'Select_txt_radio',
    'priority' => 1,
    'choices' => array(
        'ption1' => 'ption A',
        'ption2' => 'ption B',
        'ption3' => 'ption C',
        ),
    )
);
    
    

// By selecting any option, the following terms apply
    
$select_control = get_theme_mod( 'setting_radio_main' );  // Radio configuration ID
    
  if( $select_control === 'ption1' ) { 

// Display the following settings if you select the first option
      
$wp_customize->add_setting( 'setting_radio_ption1', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption1',
   array(
     
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption1' ),
      ),
       
   )
);
      
return;
      
  } elseif( $select_control === 'ption2' ) {
      
// Display the following settings if you choose the second option
      
$wp_customize->add_setting( 'setting_radio_ption2', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption2',
   array(
     
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption2' ),
      ),
       
   )
);
      
return;
      
  } elseif( $select_control === 'ption3' ) {
      
      
// Display the following settings if you choose the third option
      
$wp_customize->add_setting( 'setting_radio_ption3', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption3',
   array(
      
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption3' ),
      ),
       
   )
);
      
return;
      
  } else{
      
// Display the following settings if no option is active 
      
$wp_customize->add_setting( 'setting_radio_ptionNon', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
 ) 
);

      
$wp_customize->add_control( 'setting_radio_ptionNon',
   array(

      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'default' ),
      ),
   )
);
                                                           
return;
      
  }

}
add_action( 'customize_register', 'customize_pro' );

字符串

编辑:

我知道页面将被刷新,代码为'transport' => 'refresh'
但是当页面刷新时,所需选项的设置不会出现。
选择一个选项后,我必须单击自定义中的发布按钮。然后通过完整的浏览器刷新实时预览页面(通过更新其地址刷新页面);在这种情况下,我可以看到该选项的设置。
我正在寻找一个解决方案,以便当选择一个选项时,其设置将立即出现,而无需单击发布按钮并通过浏览器刷新页面。
我写的代码有一些错误,我需要帮助修复,这段代码只是一个粗略的想法。

zpqajqem

zpqajqem1#

在添加每个条件设置时,您需要使用active_callback参数。此外,不确定ption是否是代码示例中option的拼写错误。

$wp_customize->add_setting( 'setting_radio_ption1', 
 array( 
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',
    'active_callback' => 'my_conditional_check'
 ) 
);

function my_conditional_check() {
  return get_theme_mod( 'setting_radio_main' ) === 'ption1';
}

字符串

hgb9j2n6

hgb9j2n62#

我能够通过搜索互联网和使用以下教程解决我的问题:
What’s new with the Customizer
完整的编辑代码:

**注意:**我下面写的代码并不完全是我在上面的问题中写的代码。此代码已经过编辑,测试和工作。

function customize_pro($wp_customize){

$wp_customize->add_section('Select_txt_radio', array(
    'title' => esc_html__('Show or hide settings', 'section'),
    'priority' => 5,
));  
    
    

// Create a radio controller
    
$wp_customize->add_setting( 'setting_radio_main', 
 array(
     
    'default' => 'ption2',
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',  //postMessage
     
 ) 
);
    

    
$wp_customize->add_control('setting_radio_main', 
array(
    
    'type' => 'radio',
    'label' => esc_html__('Available options'),
    'description' => esc_html__('By selecting each option, the corresponding settings will appear'),
    'section' => 'Select_txt_radio',
    'priority' => 1,
    'choices' => array(
        'ption1' => 'ption A',
        'ption2' => 'ption B',
        'ption3' => 'ption C',
        ),
    )
);
    
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    // Creating the controller related to option 1
    
    $wp_customize->add_setting( 'setting_radio_ption1', 
 array( 
     
    'type'       => 'theme_mod', 
     'transport'  => 'refresh',  //postMessage
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption1',
   array(
     
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption1' ),
      ),
       'active_callback' => 'ption1_callback', // Connection to the function written for option 1 (controller display condition)
       
   )
);
    
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    // Creating the controller related to option 2
    
    $wp_customize->add_setting( 'setting_radio_ption2', 
 array( 
     
    'type'       => 'theme_mod', 
     'transport'  => 'refresh',  //postMessage
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption2',
   array(
     
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption2' ),
      ),
       'active_callback' => 'ption2_callback', // Connection to the function written for option 2 (controller display condition)
       
   )
);
    
    
    
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    // Creating the controller related to option 3
    
    
    
    $wp_customize->add_setting( 'setting_radio_ption3', 
 array( 
     
    'type'       => 'theme_mod', 
    'transport'  => 'refresh',  //postMessage
 ) 
);
    

$wp_customize->add_control( 'setting_radio_ption3',
   array(
      
      'section' => 'Select_txt_radio',
      'type' => 'text',
      'input_attrs' => array(
         'placeholder' => __( 'ption3' ),
      ),
        'active_callback' => 'ption3_callback',  // Connection to the function written for option 3 (controller display condition)
       
   )
);

}
add_action( 'customize_register', 'customize_pro' );


////////////////////////////////////////////////////////////////////////////////////////////////

// Functions to display controller with options selection

function ption1_callback( $control ) {
   if ( $control->manager->get_setting('setting_radio_main')->value() == 'ption1' ) {
      return true;
   } else {
      return false;
   }
}

function ption2_callback( $control ) {
   if ( $control->manager->get_setting('setting_radio_main')->value() == 'ption2' ) {
      return true;
   } else {
      return false;
   }
}


function ption3_callback( $control ) {
   if ( $control->manager->get_setting('setting_radio_main')->value() == 'ption3' ) {
      return true;
   } else {
      return false;
   }
}

字符串

我希望这个答案对正在寻找解决这个问题的方法的朋友有用。

相关问题