<?php
if ( have_posts() ) :
if (is_blog()) {
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$args['post_status'] = 'publish';
$args['category_name'] = 'services, categories';
query_posts( $args );
}
/* Start the Loop */
echo '<div><div id="response">';
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content/news-card', get_post_format() );
endwhile;
echo '</div>';
if ( $wp_query->max_num_pages > 1 ) :
echo '<div id="misha_loadmore" class="basic">More posts</div>'; // you can use <a> as well
endif;
echo '</div>';
// the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
$("body").on("click", "#misha_loadmore", function() {
var filter = $('#filter');
console.log('Кнопка точно работает');
$.ajax({
url : misha_loadmore_params.ajaxurl, // AJAX handler
data : {
'action': 'loadmore', // the parameter for admin-ajax.php
'query': misha_loadmore_params.posts, // loop parameters passed by wp_localize_script()
'page' : misha_loadmore_params.current_page // current page
},
type : 'POST',
beforeSend : function ( xhr ) {
$('#misha_loadmore').text('Loading...'); // some type of preloader
},
success : function( data ){
if( data ) {
$('#misha_loadmore').text( 'More posts' );
$('#response').append( data ); // insert new posts
misha_loadmore_params.current_page++;
if ( misha_loadmore_params.current_page == misha_loadmore_params.max_page )
$('#misha_loadmore').hide(); // if last page, HIDE the button
} else {
$('#misha_loadmore').hide(); // if no data, HIDE the button as well
}
}
});
return false;
});
add_action( 'wp_enqueue_scripts', 'misha_script_and_styles', 1 );
function misha_script_and_styles() {
global $wp_query;
wp_register_script( 'misha_scripts', get_stylesheet_directory_uri() . '/script.js', array('jquery') );
wp_localize_script( 'misha_scripts', 'misha_loadmore_params', array(
'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
'posts' => json_encode( $wp_query->query_vars ), // everything about your loop is here
'current_page' => $wp_query->query_vars['paged'] ? $wp_query->query_vars['paged'] : 1,
'max_page' => $wp_query->max_num_pages
) );
wp_enqueue_script( 'misha_scripts' );
}
add_action('wp_ajax_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'misha_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}
function misha_loadmore_ajax_handler(){
// prepare our arguments for the query
$form_filter_checkboxes = array($_POST['categoryfilter-1'], $_POST['categoryfilter-2']);
$args = json_decode( stripslashes( $_POST['query'] ), true );
$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
$args['post_status'] = 'publish';
$args['category_name'] = 'services, categories';
// it is always better to use WP_Query but not here
query_posts( $args );
if( have_posts() ) :
// run the loop
while( have_posts() ): the_post();
get_template_part( 'template-parts/content/news-card', get_post_format() );
endwhile;
endif;
die; // here we exit the script and even no wp_reset_query() required!
}
document.querySelectorAll('.productItem').forEach(n => {
const slider = new Swiper(n.querySelector('.product-top'), {
navigation: {
nextEl: n.querySelector('.swiper-button-next'),
prevEl: n.querySelector('.swiper-button-prev'),
},
spaceBetween: 10,
});
const thumbs = new Swiper(n.querySelector('.product-thumbs'), {
spaceBetween: 10,
centeredSlides: true,
slidesPerView: 'auto',
touchRatio: 0.2,
slideToClickedSlide: true,
slidesPerView: 3,
});
slider.controller.control = thumbs;
thumbs.controller.control = slider;
});