2017-04-12 5 views
0

Meine aktuelle Abfrage istVerwenden Ajax in Wordpress

$args=array(
    'post_type'=>'product', 
    'post_per_page'=>'10', 
    'tax_query' => array(
      array(
      'taxonomy' => 'product_cat', 
      'field' => 'slug', 
      'terms' => 'car', 
       ), 
         ), 
      ); 
    $loop=new WP_Query($args); 

und ich habe beim Check-Boxen Bus<input type="checkbox" class="ve-select" value="bus">

Boat<input type="checkbox" class="ve-select" value="boat">.

Momentan, wenn ich auf ein Kontrollkästchen klicke, lade ich die Seite mit JavaScript neu und übergebe den Wert durch 've' Variable.

$args=array(
    'post_type'=>'product', 
    'post_per_page'=>'10', 
    'tax_query' => array(
      array(
      'taxonomy' => 'product_cat', 
      'field' => 'slug', 
      'terms' => $_REQUEST['ve'], 
       ), 
         ), 
      ); 
    $loop=new WP_Query($args); 

Bitte irgendjemand vorschlagen, wie kann ich die Abfrage ändern und das Ergebnis ohne Neuladen zeigen.

Bitte helfen Sie Freunden.

Antwort

1

Bitte versuchen Sie den folgenden Code ::

in Ihr post_page

<script> 
    function getPostList(val) 
    { 
     jQuery.ajax({ 
      url: '<?= admin_url('admin-ajax.php') ?>', 
      data: {action:'my_action', term: val, secret: '<?php wp_create_nonce('listPost_byCat'); ?>'}, 
      method: 'POST', 
      success: function(res){ 
       jQuery('#post-div').html(res); 
      } 
     }); 
    } 

    jQuery(document).ready(function(){ 
     jQuery('.ve-select).on('click', function(){ 
      getPostList(jQuery(this).val()); 
     }); 
    }); 
</script> 

in functions.php

<?php 
    add_action('wp_ajax_my_action', 'my_action_handler'); 
    add_action('wp_ajax_nopriv_my_action', 'my_action_handler'); 

    function my_action_handler() 
    { 
     if(wp_verify_nonce($_POST['secret'], 'listPost_byCat')) 
     { 
      $args=array(
       'post_type'=>'product', 
       'post_per_page'=>'10', 
       'tax_query' => array(
         array(
          'taxonomy' => 'product_cat', 
          'field' => 'slug', 
          'terms' => $_POST['term'], 
         ), 
        ), 
       ); 
      $loop=new WP_Query($args); 

      if(!empty($loop)) 
      { 
       foreach($loop as $post) 
       { 
        echo $post->post_title() . '<br/>'; 
       } 
      } 
     } 
    } 
?> 
+0

i wird diese Lösung überprüfen – Manik

+0

Sie Freund danken. Das funktioniert. – Manik

+0

Aber ich weiß das nicht. secret: ' Manik