Advertisement
nefi_c

custom ajax slide query

Feb 7th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.67 KB | None | 0 0
  1. function my_login_wpcategories(){
  2.     global $wpdb;
  3.     $slide = 1;
  4.     $popup = 1;
  5.     $ajax_content = '';
  6.    
  7.     $taxonomy = 'wpcategories';
  8.     $parent_term = $_GET['term'];
  9.    
  10.     $parent_term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug='$parent_term'");
  11.     $child_term = get_term_children( $parent_term_id , $taxonomy );
  12.    
  13.     foreach($child_term as $term){ 
  14.        
  15.         if($slide==1 || $slide%3==1)
  16.             $ajax_content .= '<div class="slide">';
  17.        
  18.         $term_data = get_term_by('id', $term, $taxonomy);
  19.         $args = array(
  20.             'post_type' => 'whitepaper',
  21.             'tax_query' => array(
  22.                 array(
  23.                     'taxonomy'  => $taxonomy,
  24.                     'field'     => 'id',
  25.                     'terms'     =>  $term
  26.                 )
  27.             ),
  28.             'posts_per_page' => -1
  29.         );
  30.        
  31.         $tabpopup = $slidebase = '';
  32.         $pop_i = 1;
  33.         $my_post = new WP_Query($args);
  34.        
  35.         // .processitem
  36.         $ajax_content .= '<div class="processitem">';
  37.        
  38.         if($my_post->have_posts()) :
  39.             while ( $my_post->have_posts() ) : $my_post->the_post();
  40.                 if($pop_i<=5)
  41.                     $tabpopup_content .= '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
  42.                 $pop_i++;
  43.                 $slidebase_content .= '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
  44.             endwhile;
  45.            
  46.             $tabpopup .= '<div id="pop'.str_pad( $popup, 2, 0, STR_PAD_LEFT ).'" class="tabpopup" style="display: none;"><h3>'.$term_data->name.'</h3><div>';
  47.             $tabpopup .= $tabpopup_content;
  48.             $tabpopup .= '</div></div>';
  49.            
  50.             $slidebase .= '<div class="slidebase noborder tabber"><h3>'.$term_data->name.'</h3><div>';
  51.             $slidebase .= $slidebase_content;
  52.             $slidebase .= '</div><p><a href="#" id="p'.str_pad( $popup, 2, 0, STR_PAD_LEFT ).'" class="mediumgraybutton">More</a></p></div>';
  53.            
  54.             $ajax_content .= $tabpopup;
  55.             $ajax_content .= $slidebase;
  56.            
  57.         endif;
  58.        
  59.         // .processitem
  60.         $ajax_content .= '</div>'; 
  61.        
  62.         if($slide%3==0)
  63.             $ajax_content .= '</div><!--slide-->';
  64.        
  65.         $slide++; $popup++;
  66.     }
  67.    
  68.     if(($slide-1)%3!=0)
  69.         $ajax_content .= '</div><!--slide-->'; 
  70.  
  71.     echo $ajax_content;
  72.     die;
  73. }
  74.  
  75. function my_nonlogin_wpcategories(){
  76.     global $wpdb;
  77.     $slide = 1;
  78.     $popup = 1;
  79.     $ajax_content = '';
  80.     $register_url = 'http://yoursite.com/register';
  81.    
  82.     $taxonomy = 'wpcategories';
  83.     $parent_term = $_GET['term'];
  84.    
  85.     $parent_term_id = $wpdb->get_var("SELECT term_id FROM $wpdb->terms WHERE slug='$parent_term'");
  86.     $child_term = get_term_children( $parent_term_id , $taxonomy );
  87.    
  88.     foreach($child_term as $term){ 
  89.        
  90.         if($slide==1 || $slide%3==1)
  91.             $ajax_content .= '<div class="slide">';
  92.        
  93.         $term_data = get_term_by('id', $term, $taxonomy);
  94.         $args = array(
  95.             'post_type' => 'whitepaper',
  96.             'tax_query' => array(
  97.                 array(
  98.                     'taxonomy'  => $taxonomy,
  99.                     'field'     => 'id',
  100.                     'terms'     =>  $term
  101.                 )
  102.             ),
  103.             'posts_per_page' => -1
  104.         );
  105.        
  106.         $tabpopup = $slidebase = '';
  107.         $pop_i = 1;
  108.         $my_post = new WP_Query($args);
  109.        
  110.         // .processitem
  111.         $ajax_content .= '<div class="processitem">';
  112.        
  113.         if($my_post->have_posts()) :
  114.             while ( $my_post->have_posts() ) : $my_post->the_post();
  115.                 if($pop_i<=5)
  116.                     $tabpopup_content .= '<p><a href="'.$register_url.'">'.get_the_title().'</a></p>';
  117.                 $pop_i++;
  118.                 $slidebase_content .= '<p><a href="'.$register_url.'">'.get_the_title().'</a></p>';
  119.             endwhile;
  120.            
  121.             $tabpopup .= '<div id="pop'.str_pad( $popup, 2, 0, STR_PAD_LEFT ).'" class="tabpopup" style="display: none;"><h3>'.$term_data->name.'</h3><div>';
  122.             $tabpopup .= $tabpopup_content;
  123.             $tabpopup .= '</div></div>';
  124.            
  125.             $slidebase .= '<div class="slidebase noborder tabber"><h3>'.$term_data->name.'</h3><div>';
  126.             $slidebase .= $slidebase_content;
  127.             $slidebase .= '</div><p><a href="#" id="p'.str_pad( $popup, 2, 0, STR_PAD_LEFT ).'" class="mediumgraybutton">More</a></p></div>';
  128.            
  129.             $ajax_content .= $tabpopup;
  130.             $ajax_content .= $slidebase;
  131.            
  132.         endif;
  133.        
  134.         // .processitem
  135.         $ajax_content .= '</div>'; 
  136.        
  137.         if($slide%3==0)
  138.             $ajax_content .= '</div>';
  139.        
  140.         $slide++; $popup++;
  141.     }
  142.    
  143.     if(($slide-1)%3!=0)
  144.         $ajax_content .= '</div><!--slide-->';
  145.        
  146.     echo $ajax_content;
  147.     die;
  148. }
  149. add_action('wp_ajax_wpcategories_action', 'my_login_wpcategories');
  150. add_action('wp_ajax_nopriv_wpcategories_action', 'my_nonlogin_wpcategories');
  151.  
  152. // enqueue our ajax script file and passing ajaxurl value
  153. function my_ajax_load_scripts() {
  154.     // get our custom-script.js
  155.     wp_enqueue_script('my-custom-script', get_template_directory_uri() . '/js/custom-script.js');
  156.     // globalize admin-ajax.php url for ajax processing page
  157.     wp_localize_script('my-custom-script', 'my_wp', array(
  158.             'ajaxurl' => admin_url('admin-ajax.php'),
  159.             'action'  => 'wpcategories_action'
  160.         )
  161.     );
  162. }
  163. add_action('wp_enqueue_scripts', 'my_ajax_load_scripts');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement