2017-08-30 55 views
1

我正在開發一個自定義小部件,我想有一個下拉菜單,我可以選擇其中一個自定義帖子類型的3 availabe自定義分類法。 該選擇應提供第二個操作將在另一個下拉菜單中列出所選分類的所有術語的分類。從一個下拉變量的Wordpress小部件查詢

這是我做了什麼:

<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

您不能在使用PHP的select事件上使用條件邏輯。要麼你需要得到所有選擇&選項,並告訴他們有條件(不推薦使用的方式)

OR

您需要在第一個選擇框中更改添加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(); 
      } 

希望這有助於。 (這是在我的系統上測試的代碼) 謝謝。

+0

對不起,可能我是以錯誤的方式使用它,但對於您的解決方案,我可以直觀地看到第一個下拉菜單 –

+0

如果發生火災,我已經添加了警報,請檢查控制檯。它應該給你選擇框的迴應。如果警報不是可能觸發,這是js問題。 –

+0

我實際上可以看到警報,但是一旦我點擊「確定」,我可以在控制檯上看到一個錯誤,並且沒有其他下拉菜單出現 這是我可以看到的:http://imgur.com/a/aK9Us –