Advertisement
Evengar

Untitled

Feb 27th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.01 KB | None | 0 0
  1. //load custom ajax handler
  2. add_action( 'wp_enqueue_scripts', 'myajax_data', 99 );
  3. function myajax_data(){
  4.  
  5.     wp_localize_script('app', 'myajax',
  6.         array(
  7.             'url' => admin_url('admin-ajax.php')
  8.         )
  9.     );  
  10.  
  11. }
  12.  
  13. add_action('wp_footer', 'my_action_javascript', 99); // для фронта
  14. function my_action_javascript() {
  15.     ?>
  16.     <script type="text/javascript" >
  17.     jQuery(document).ready(function($) {
  18.         var page_number = 1;
  19.         jQuery(".acf_selectors").change(function load_acf_posts(){
  20.             usluga_value = jQuery("#acf_uslugi").attr('value');
  21.             material_value = jQuery("#acf_material").attr('value');
  22.             tolshina_value = jQuery("#acf_tolshina").attr('value');
  23.             tag_value = jQuery("#tag_value_hidden").text();
  24.             page_number = 1;          
  25.             //console.log('ok');
  26.             var data = {
  27.                 action: 'my_action',
  28.                 tag: tag_value,
  29.                 usluga: usluga_value,
  30.                 material: material_value,
  31.                 tolshina: tolshina_value,
  32.                 paged: page_number,
  33.             };            
  34.             //console.log(data);
  35.  
  36.             jQuery.post( myajax.url, data, function(response) {
  37.                 //console.log(response);
  38.                 jQuery(".sibrez_ajax_posts_wrapper").empty();
  39.                 jQuery(".sibrez_ajax_posts_wrapper").append(response);
  40.                 jQuery('.gallery').flickity({
  41.                     wrapAround:true,
  42.                     cellAlign: 'left',
  43.                     pageDots:false
  44.                 });                
  45.             });            
  46.         });
  47.  
  48.         jQuery("#acf_load_more").click(function acf_load_more(){
  49.             page_number++;
  50.             //console.log(page_number);
  51.  
  52.             usluga_value = jQuery("#acf_uslugi").attr('value');
  53.             material_value = jQuery("#acf_material").attr('value');
  54.             tolshina_value = jQuery("#acf_tolshina").attr('value');
  55.             tag_value = jQuery("#tag_value_hidden").text();  
  56.  
  57.             //console.log('ok');
  58.             var data = {
  59.                 action: 'my_action',
  60.                 tag: tag_value,
  61.                 usluga: usluga_value,
  62.                 material: material_value,
  63.                 tolshina: tolshina_value,
  64.                 paged: page_number,
  65.             };
  66.  
  67.             //console.log(data);
  68.  
  69.             jQuery.post( myajax.url, data, function(response) {
  70.                 //console.log(response);
  71.                 //jQuery(".sibrez_ajax_posts_wrapper").empty();
  72.                 jQuery(".sibrez_ajax_posts_wrapper").append(response);
  73.                 jQuery('.gallery').flickity({
  74.                     wrapAround:true,
  75.                     cellAlign: 'left',
  76.                     pageDots:false
  77.                 });                
  78.             });  
  79.  
  80.         });
  81.  
  82.     });
  83.     </script>
  84.     <?php
  85. }
  86.  
  87. add_action('wp_ajax_my_action', 'my_action_callback');
  88. add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
  89.  
  90. function my_action_callback($tag='', $usluga='', $material='', $tolshina='', $paged=1) {
  91.     $tag = $_POST['tag'];
  92.     $usluga = $_POST['usluga'];
  93.     $material = $_POST['material'];
  94.     $tolshina = $_POST['tolshina'];
  95.     $paged = $_POST['paged'];
  96.  
  97.     //echo $usluga;
  98.  
  99.     if (!empty($usluga)) {
  100.         $usluga_query =  array(
  101.                     'key'       => 'usluga',
  102.                     'value'     => $usluga,
  103.                     'compare'   => 'LIKE',
  104.                 );
  105.     }
  106.  
  107.     if (!empty($material)) {
  108.         $material_query =  array(
  109.                     'key'       => 'material',
  110.                     'value'     => $material,
  111.                     'compare'   => '=',
  112.                 );
  113.     }
  114.  
  115.     if (!empty($tolshina)) {
  116.         $tolshina_query =  array(
  117.                     'key'       => 'tolshina',
  118.                     'value'     => $tolshina,
  119.                     'compare'   => 'LIKE',
  120.                 );
  121.     }
  122.  
  123.     //echo $usluga;
  124.     $query_args = array(
  125.         'post_type' => 'post',
  126.         'category_name' => 'news',
  127.         'posts_per_page' => 15,
  128.         'paged' => $paged,
  129.         'tag'   => $tag,
  130.         //set up meta query
  131.         'meta_query'    => array(
  132.                 'relation'      => 'AND',
  133.                 $usluga_query,
  134.                 $material_query,
  135.                 $tolshina_query,                      
  136.             ),          
  137.  
  138.     );
  139.  
  140.     $custom_query = new WP_Query($query_args);
  141.     $gallery_id = 1;
  142.     $exclude_posts = '';
  143.  
  144.     if ( $custom_query->have_posts() ) : while($custom_query->have_posts()) : $custom_query->the_post();
  145.         $exclude_posts .= $post->ID.',';
  146.  
  147.         $class = '';
  148.  
  149.         // Add new style class for all news
  150.         $class .= 'news-new-style';
  151.         $new_style_post = true;
  152.  
  153.         if (has_post_thumbnail()) {
  154.             $class .= ' isimg';
  155.         }
  156.  
  157.         $attachment_ids = get_gallery_ids_from_post();
  158.         $tags = wp_get_post_tags($post->ID, array('orderby ' => 'term_order'));
  159.         $title = '';
  160.  
  161.         if($tags) {
  162.             foreach ($tags as $tag) {
  163.                 $title .= '<a href="/news/?tag=' . $tag->slug . '">#' . $tag->name . '</a> ';
  164.             }
  165.         }
  166.         $title .= ' ' . the_title('', '', false);
  167.         ?>
  168.  
  169.             <?php $output .=
  170.             "<div class=\"item-post $class\">
  171.                <div class=\"date\">" . get_the_date('d F Y') . "</div>
  172.                    <h2>$title</h2>
  173.                <div class=\"img-post\">" .
  174.                     do_shortcode('[gallery size="gallery-thumb2" columns="3" ids="'.$attachment_ids.'" link="file" ]') .
  175.                 "</div>
  176.            </div>
  177.            </div>
  178.            "; ?>
  179.  
  180.     <?php $gallery_id++; endwhile; ?>
  181.     <?php wp_reset_postdata(); ?>
  182.     <?php endif; ?>
  183.     <?php
  184.     // выход нужен для того, чтобы в ответе не было ничего лишнего, только то что возвращает функция
  185.     wp_die($output);
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement