So I’m running Sage 9 and creating an AJAX load more news script that works except for whenever it returns new posts it adds a “1” at the end of the response and I can’t understand why.
This is my javascript:
$(document).ready(function() {
let currentPage = 1;
$('#load-more').on('click', function() {
currentPage++;
$.ajax({
type: 'POST',
url: '/wp-admin/admin-ajax.php',
dataType: 'html',
data: {
action: 'load_more_posts',
paged: currentPage,
},
success: function (res) {
$('.publication-list').append(res);
}
});
})
});
And this is my function:
function load_more_posts() {
$ajaxposts = new WP_Query([
'post_type' => 'nyheter',
'posts_per_page' => 6,
'orderby' => 'date',
'order' => 'DESC',
'paged' => $_POST['paged'],
]);
$response = '';
if($ajaxposts->have_posts()) {
while($ajaxposts->have_posts()) : $ajaxposts->the_post();
$response .= include \App\template_path(locate_template('views/partials/cardNews.blade.php'));
endwhile;
} else {
$response = '';
}
echo $response;
exit;
}
add_action('wp_ajax_load_more_posts', 'load_more_posts');
add_action('wp_ajax_nopriv_load_more_posts', 'load_more_posts');
Whenever I click “load more” it returns 6 news as it should but it ends with “111111” (one 1 for every post). Can’t for my life understand why. if I change the function from this:
$response .= include \App\template_path(locate_template('views/partials/cardNews.blade.php'));
To this:
$response = include \App\template_path(locate_template('views/partials/cardNews.blade.php'));
It will only return one “1” at the end. But I don’t understand where it comes from and how to get rid of it. Does anyone have any ideas or how I could solve it? Everything works great otherwise.
Image of the return:
Thanks