nefi_c

ajax callback wp_query

Feb 5th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. function your_callback_to_return_content(){
  4.     $cat = $_POST['category'];
  5.    
  6.     $args = array(
  7.         'category_name'=> $cat,
  8.         'posts_per_page' => -1
  9.     );
  10.    
  11.     $slide = 0;
  12.     $ajax_content = '';
  13.     $my_post = new WP_Query($args);
  14.    
  15.     if($my_post->have_posts()) :
  16.      
  17.       $ajax_content .= '<div id="#whitepapersshow">';
  18.        
  19.         while ( $my_post->have_posts() ) :
  20.        
  21.         $my_post->the_post();
  22.    
  23.         $ajax_content .= '<h2><a href="'.get_the_permalink().'">'.get_the_title().'</a></h2>';
  24.    
  25.         endwhile;
  26.        
  27.       $ajax_content .= '</div>';
  28.      
  29.     endif;
  30.    
  31.     echo $ajax_content;
  32.     die;
  33. }
  34. add_action('wp_ajax_my_action', 'your_callback_to_return_content');
  35. add_action('wp_ajax_nopriv_my_action', 'your_callback_to_return_content');
  36.  
  37. // enqueue our ajax script file and passing ajaxurl value
  38. function my_ajax_load_scripts() {
  39.     // get our custom-script.js
  40.     wp_enqueue_script('my-custom-script', get_template_directory_uri() . '/js/custom-script.js');
  41.     // globalize admin-ajax.php url for ajax processing page
  42.     wp_localize_script('my-custom-script', 'my_wp', array(
  43.             'ajaxurl' => admin_url('admin-ajax.php')
  44.         )
  45.     );
  46. }
  47. add_action('wp_enqueue_scripts', 'my_ajax_load_scripts');
  48.  
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment