Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //load custom ajax handler
- add_action( 'wp_enqueue_scripts', 'myajax_data', 99 );
- function myajax_data(){
- wp_localize_script('app', 'myajax',
- array(
- 'url' => admin_url('admin-ajax.php')
- )
- );
- }
- add_action('wp_footer', 'my_action_javascript', 99); // для фронта
- function my_action_javascript() {
- ?>
- <script type="text/javascript" >
- jQuery(document).ready(function($) {
- var page_number = 1;
- jQuery(".acf_selectors").change(function load_acf_posts(){
- usluga_value = jQuery("#acf_uslugi").attr('value');
- material_value = jQuery("#acf_material").attr('value');
- tolshina_value = jQuery("#acf_tolshina").attr('value');
- tag_value = jQuery("#tag_value_hidden").text();
- page_number = 1;
- //console.log('ok');
- var data = {
- action: 'my_action',
- tag: tag_value,
- usluga: usluga_value,
- material: material_value,
- tolshina: tolshina_value,
- paged: page_number,
- };
- //console.log(data);
- jQuery.post( myajax.url, data, function(response) {
- //console.log(response);
- jQuery(".sibrez_ajax_posts_wrapper").empty();
- jQuery(".sibrez_ajax_posts_wrapper").append(response);
- jQuery('.gallery').flickity({
- wrapAround:true,
- cellAlign: 'left',
- pageDots:false
- });
- });
- });
- jQuery("#acf_load_more").click(function acf_load_more(){
- page_number++;
- //console.log(page_number);
- usluga_value = jQuery("#acf_uslugi").attr('value');
- material_value = jQuery("#acf_material").attr('value');
- tolshina_value = jQuery("#acf_tolshina").attr('value');
- tag_value = jQuery("#tag_value_hidden").text();
- //console.log('ok');
- var data = {
- action: 'my_action',
- tag: tag_value,
- usluga: usluga_value,
- material: material_value,
- tolshina: tolshina_value,
- paged: page_number,
- };
- //console.log(data);
- jQuery.post( myajax.url, data, function(response) {
- //console.log(response);
- //jQuery(".sibrez_ajax_posts_wrapper").empty();
- jQuery(".sibrez_ajax_posts_wrapper").append(response);
- jQuery('.gallery').flickity({
- wrapAround:true,
- cellAlign: 'left',
- pageDots:false
- });
- });
- });
- });
- </script>
- <?php
- }
- add_action('wp_ajax_my_action', 'my_action_callback');
- add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
- function my_action_callback($tag='', $usluga='', $material='', $tolshina='', $paged=1) {
- $tag = $_POST['tag'];
- $usluga = $_POST['usluga'];
- $material = $_POST['material'];
- $tolshina = $_POST['tolshina'];
- $paged = $_POST['paged'];
- //echo $usluga;
- if (!empty($usluga)) {
- $usluga_query = array(
- 'key' => 'usluga',
- 'value' => $usluga,
- 'compare' => 'LIKE',
- );
- }
- if (!empty($material)) {
- $material_query = array(
- 'key' => 'material',
- 'value' => $material,
- 'compare' => '=',
- );
- }
- if (!empty($tolshina)) {
- $tolshina_query = array(
- 'key' => 'tolshina',
- 'value' => $tolshina,
- 'compare' => 'LIKE',
- );
- }
- //echo $usluga;
- $query_args = array(
- 'post_type' => 'post',
- 'category_name' => 'news',
- 'posts_per_page' => 15,
- 'paged' => $paged,
- 'tag' => $tag,
- //set up meta query
- 'meta_query' => array(
- 'relation' => 'AND',
- $usluga_query,
- $material_query,
- $tolshina_query,
- ),
- );
- $custom_query = new WP_Query($query_args);
- $gallery_id = 1;
- $exclude_posts = '';
- if ( $custom_query->have_posts() ) : while($custom_query->have_posts()) : $custom_query->the_post();
- $exclude_posts .= $post->ID.',';
- $class = '';
- // Add new style class for all news
- $class .= 'news-new-style';
- $new_style_post = true;
- if (has_post_thumbnail()) {
- $class .= ' isimg';
- }
- $attachment_ids = get_gallery_ids_from_post();
- $tags = wp_get_post_tags($post->ID, array('orderby ' => 'term_order'));
- $title = '';
- if($tags) {
- foreach ($tags as $tag) {
- $title .= '<a href="/news/?tag=' . $tag->slug . '">#' . $tag->name . '</a> ';
- }
- }
- $title .= ' ' . the_title('', '', false);
- ?>
- <?php $output .=
- "<div class=\"item-post $class\">
- <div class=\"date\">" . get_the_date('d F Y') . "</div>
- <h2>$title</h2>
- <div class=\"img-post\">" .
- do_shortcode('[gallery size="gallery-thumb2" columns="3" ids="'.$attachment_ids.'" link="file" ]') .
- "</div>
- </div>
- </div>
- "; ?>
- <?php $gallery_id++; endwhile; ?>
- <?php wp_reset_postdata(); ?>
- <?php endif; ?>
- <?php
- // выход нужен для того, чтобы в ответе не было ничего лишнего, только то что возвращает функция
- wp_die($output);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement