我正在开发一个自定义小部件,我想有一个下拉菜单,我可以选择其中一个自定义帖子类型的3个可用自定义分类。 此选择应提供第二个操作列出的分类 - 在另一个下拉菜单中 - 所选分类的所有条款。
这就是我所做的:
<div class="widget-option">
<div class="widget-th">
<label for="<?php echo esc_attr( $this->get_field_id( 'selezionato' ) ); ?>"><b><?php _e( 'Limit items', 'msd' ); ?></b></label>
</div>
<div class="widget-td">
<select id="<?php echo esc_attr( $this->get_field_id( 'selezionato' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'selezionato' ) ); ?>">
<option><?php _e( 'Seleziona la tipologia', 'msd' ); ?></option>
<option value="taxonomy_slug_1" <?php selected( $selezionato, "taxonomy_slug_1" ); ?>><?php _e( 'Taxonomy 1', 'msd' ); ?></option>
<option value="taxonomy_slug_2" <?php selected( $selezionato, "taxonomy_slug_2" ); ?>><?php _e( 'Taxonomy 2', 'msd' ); ?></option>
<option value="taxonomy_slug_3" <?php selected( $selezionato, "taxonomy_slug_3" ); ?>><?php _e( 'Taxonomy 3', 'msd' ); ?></option>
</select>
<select id="<?php echo esc_attr( $this->get_field_id( 'valori' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'valori' ) ); ?>">
<?php $cats = get_terms($instance['selezionato' ]);?>
<?php
foreach($cats as $cat){ ?>
<option value="<?php echo $cat->slug; ?>" <?php selected( $instance['valori'], $cat->slug ); ?>><?php _e( $cat->name,'msd' ); ?></option>
<?php } ?>
</select>
<p><?php _e( 'This field is optional', 'msd' ); ?></p>
</div>
<div class="clearfix"></div>
</div>
我缺少什么?
答案 0 :(得分:0)
您不能在使用PHP的select事件上使用条件逻辑。要么你需要得到所有选择和选项并有条件地显示它们(不是首选方式)
或强>
您需要在首次选择框更改时添加AJAX调用。
按照:
/* first select box */
<select id="select_d" name="<?php echo esc_attr( $this->get_field_name( 'selezionato' ) ); ?>">
<option class="sd">select</option>
<option value="taxonomy_slug_1">Taxonomy 1</option>/*use as same as you done above.*/
<option value="taxonomy_slug_2">Taxonomy 2</option>
<option value="taxonomy_slug_3">Taxonomy 3</option>
</select>
<div class="resresult"></div>
/*JQuery for getting taxonomy on change event*/
<script type="text/javascript">
jQuery('#select_d').on('change', function(){
var tax = jQuery("#select_d").val();
alert(tax)
jQuery.ajax({
url: myScript.ajax_url,
type: "POST",
data: {
action: 'get_more_select',
tax: tax,
},
success: function(response){
console.log(response)
jQuery('.resresult').html(response);
}
});
})
</script>
/*Enqueue your script (if not added)*/
add_action('wp_enqueue_scripts','ajaxlar_js_fn');
function ajaxlar_js_fn() {
wp_enqueue_script( 'ajaxlar_js', plugins_url( '/js/custom.js', __FILE__ ), true);
wp_localize_script('ajaxlar_js', 'myScript', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
));
}
/*Finally get the result and return to blank div we had created after first select box*/
add_action('wp_ajax_nopriv_more_posts', 'get_more_select');
function get_more_select(){
$tax = $_POST['tax'];
$terms = get_terms( array(
'taxonomy' => $tax,
'hide_empty' => false,
) );
$html = "";
$html .= "<select>";
$html .= "<option>select</option>";
foreach ($terms as $value) {
$html .= "<option value='".$value->slug."'>".$value->slug."</option>";
}
$html .= "</select>";
return $html;
exit();
}
希望这有帮助。 (这是我系统上经过测试的代码) 感谢。