滚动时,我的网站上有ajax加载帖子。问题在于出现了重复的帖子。我不明白为什么会这样。只有一些帖子有重复。这是我的密码function.php:
function loadmore_get_posts(){
$paged = !empty($_POST[\'paged\']) ? $_POST[\'paged\'] : 1;
$paged++;
$args = array(
\'paged\' => $paged,
\'posts_per_page\' => $_POST[\'posts_per_page\'],
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'cat\' => $_POST[\'cats\']
);
query_posts($args);
while( have_posts() ) : the_post();
get_template_part( \'this is template\' );
endwhile;
die;
}
add_action(\'wp_ajax_loadmore\', \'loadmore_get_posts\');
add_action(\'wp_ajax_nopriv_loadmore\', \'loadmore_get_posts\');
这是普通javascript中的ajax:
let ajaxurl = \'<?php echo admin_url(\'admin-ajax.php\') ?>\';
let section_posts = 1;
let postData = new FormData();
postData.append(\'action\', \'loadmore\');
postData.append(\'paged\', section_posts);
postData.append(\'posts_per_page\', 9);
postData.append(\'cats\', <?php print json_encode(get_selected_cats())?>);
const xhr = new XMLHttpRequest();
xhr.open(\'POST\', ajaxurl);
xhr.addEventListener(\'readystatechange\', function (data) {
if (this.readyState === 4 && this.status === 200) {
section_posts++;
document.querySelector(\'.articlefeed_template\').innerHTML += data.target.responseText;
} else {}
xhr.send(postData);
}
请告诉我我做错了什么?谢谢