我有一个简单的 ajax 调用来在我的 WordPress 网站上加载 3 个帖子,每次点击前 3 个结果时,总是跳过数据库中的第一个帖子。
Ajax 设置在第 0 页,每页添加 3 个帖子。所以我希望输出是前 3 个帖子,但是它会跳过第一个帖子并输出接下来的 3 个。然后它会从那里正常工作。
为什么会在第 0 页时跳过第一个结果?
Ajax 调用 -
var ajaxUrl = "<?php echo admin_url('admin-ajax.php')?>";
var page = 0; // What page we are on.
var ppp = 3; // Post per page
$("#more_posts").on("click",function(){ // When Load More Button is pressed.
event.preventDefault();
$("#more_posts").attr("disabled",true); // Disable the button, temp.
$.post(ajaxUrl, {
action:"more_post_ajax",
offset: (page * ppp) + 1,
ppp: ppp
}).success(function(posts){
page++;
if(posts === '') {
$("#more_posts").hide();
} else {
$(".showroom-loop").append(posts); // Which div to insert the posts
}
$("#more_posts").attr("disabled",false);
});
});
这是我的 wp args 函数 -
function more_post_ajax(){
$offset = $_POST["offset"];
$ppp = $_POST["ppp"];
header("Content-Type: text/html");
$args = array(
'post_type' => 'showroom',
'posts_per_page' => $ppp,
'offset' => $offset,
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
$loop->the_post();
echo "<h3>";the_title();echo "</h3> <hr>";
}
exit;
}
add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');
add_action('wp_ajax_more_post_ajax', 'more_post_ajax');
我认为这是因为您的代码总是在偏移量上加 1。
在您的 javascript 中,不应该是这样的:
offset: (page * ppp) + 1,
是
offset: (page * ppp),
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句