最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

taxonomy - WordPress Customizer - pass setting value into another control to live update drop-down choices

programmeradmin4浏览0评论

In the WordPress customizer script, I am trying to pass the value from a select control into a custom control that displays taxonomies associated with the selected value (1st control).

$wp_customize->add_control( new Tax_Dropdown_Control( $wp_customize, 'select_tax', array(
    'section'       => 'section_1',
    'label'         => __( 'Select Post Taxonomy', 'textdomain' ),
    'description'   => __( 'Select a taxonomy based on post type selected.', 'textdomain' ),
    'dropdown_args' => array(
        'post_type' => $wp_customize->get_setting("select_post_type"), // here I need to pass the first setting's value
    ),
) ) );

// custom controls related snippet
class Tax_Dropdown_Control extends WP_Customize_Control {
    .....    
    $dropdown_args = wp_parse_args( $this->dropdown_args, array(
        'post_type' => 'post',
    ) );
        
    $dropdown_args['echo'] = false;
                
    $taxonomies = get_object_taxonomies($dropdown_args);
                
    if  ($taxonomies) {
        echo '<select>';
        foreach ($taxonomies  as $taxonomy ) {
            echo '<option>'. $taxonomy. '</option>';
        }
        echo '</select>';
    } 
    ....
}

It would need to update the choices live when the select post type is changed. I'm not sure if maybe an active_callback can be used to recall the function with the updated variable?

In the WordPress customizer script, I am trying to pass the value from a select control into a custom control that displays taxonomies associated with the selected value (1st control).

$wp_customize->add_control( new Tax_Dropdown_Control( $wp_customize, 'select_tax', array(
    'section'       => 'section_1',
    'label'         => __( 'Select Post Taxonomy', 'textdomain' ),
    'description'   => __( 'Select a taxonomy based on post type selected.', 'textdomain' ),
    'dropdown_args' => array(
        'post_type' => $wp_customize->get_setting("select_post_type"), // here I need to pass the first setting's value
    ),
) ) );

// custom controls related snippet
class Tax_Dropdown_Control extends WP_Customize_Control {
    .....    
    $dropdown_args = wp_parse_args( $this->dropdown_args, array(
        'post_type' => 'post',
    ) );
        
    $dropdown_args['echo'] = false;
                
    $taxonomies = get_object_taxonomies($dropdown_args);
                
    if  ($taxonomies) {
        echo '<select>';
        foreach ($taxonomies  as $taxonomy ) {
            echo '<option>'. $taxonomy. '</option>';
        }
        echo '</select>';
    } 
    ....
}

It would need to update the choices live when the select post type is changed. I'm not sure if maybe an active_callback can be used to recall the function with the updated variable?

Share Improve this question edited Sep 30, 2020 at 14:49 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Aug 24, 2020 at 21:28 730wavy730wavy 1931 gold badge13 silver badges45 bronze badges 2
  • How do you add the select_post_type control? What's the code? And those echo ($taxonomies), what control/section are they in/for? – Sally CJ Commented Aug 25, 2020 at 2:20
  • @SallyCJ the select_post_type is a select control. The $taxonomies is the code for the select_tax custom control class in the render content function – 730wavy Commented Aug 25, 2020 at 4:53
Add a comment  | 

2 Answers 2

Reset to default 0

You could probably use some JavaScript to capture the selected option and then make an Ajax call to the REST API to fetch the corresponding posts and update the second field.

The customizer does everything client side, so retrieving a new taxonomy list would involve an ajax call to the rest api. That's doable, but pretty complicated. An easier, but uglier solution would be to simply include all dropdowns and toggle their display. So you would have this inside a loop through all thinkable dropdowns:

foreach [ thinkable dropdown ] {
....
if  ($taxonomies) {
    echo '<select id="' . $taxonomies . '">';
    foreach ($taxonomies  as $taxonomy ) {
        echo '<option>'. $taxonomy. '</option>';
        }
    echo '</select>';
    } 
}

This will lead to html like this:

<select id="tax1">
 <option>...</option>
 <option>...</option>
</select>
...
<select id="tax5">
 <option>...</option>
 <option>...</option>
</select>

Next, you can use the id on select to apply css to toggle the display. Obviously you don't want this ugly html on your live site, so you'll have to use is_customize_preview to make sure this is only generated when the customizer is active.

发布评论

评论列表(0)

  1. 暂无评论