Делаю бесконечный скрол, но не нашел ничего на pure js. Проблема возникает при отправке запроса к admin-ajax.php
HTML
<button
class="button button--load-more"
data-page="1"
data-url="<?php echo admin_url('admin-ajax.php'); ?>">
Показать еще
</button>
functions.php
if (wp_doing_ajax()) {
add_action('wp_ajax_nopriv_dt_load_more', 'dt_load_more_callback');
add_action('wp_ajax_dt_load_more', 'dt_load_more_callback');
}
function dt_load_more_callback() {
$paged = $_POST['page'] + 1;
$query = new WP_Query( array(
'post_type' => 'post',
'paged' => $paged,
));
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
endif;
wp_reset_postdata();
die();}
Js
function init() {
const loadButton = document.querySelector('.button--load-more');
const placeToAppend = document.querySelector('.feed__main');
function request() {
const currentPage = this.dataset.page;
const ajaxUrl = this.dataset.url;
fetch(ajaxUrl, {
method: 'POST',
body: JSON.stringify({
page: currentPage,
action: 'dt_load_more',
}),
}).then(res => res.json())// пост => контейнер
}
loadButton.addEventListener('click', request);
}
if (document.readyState === 'complete' || document.readyState === 'loaded'){
init();
} else {
document.addEventListener('DOMContentLoaded', init);
}
Headers
Request URL:http://localhost/wordpress/wp-admin/admin-ajax.php
Request Method:POST
Status Code:200 OK
Content-Type:text/html; charset=UTF-8