我有一个问题,那就是让我的自动取款机秃顶。我有一个ajax调用,它处理一个循环,该循环处理一些查询并为我返回帖子。
到目前为止还不错,但用户第一次看到页面时,我们应该加载10篇帖子,然后我们想单击一个按钮再请求5篇。
到目前为止还不错。
但当我们要求再发5个帖子时,我们又得到了前5个帖子。
我的batchloop
<?php
// Our include
define(\'WP_USE_THEMES\', false);
require_once(\'../../../wp-load.php\');
// Our variables
$posts = (isset($_GET[\'numPosts\'])) ? $_GET[\'numPosts\'] : 0;
$page = (isset($_GET[\'pageNumber\'])) ? $_GET[\'pageNumber\'] : 0;
$category = (isset($_GET[\'category_name\'])) ? $_GET[\'category_name\'] : 0;
var_dump($posts);
$args = array(
\'posts_per_page\' => $posts,
\'category_name\' => $category,
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'paged\' => $page
);
query_posts($args);
// $query = new WP_query($args);
// our loop
if (have_posts()) {
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; query_posts($args);
while (have_posts()){
the_post();
get_template_part( \'thumbs\', get_post_format() );
}
}
// unset($page, $posts, $category);
// wp_reset_postdata();
wp_reset_query();
?>
有人看到我做错了什么吗?编辑:
批处理程序
function _batchhandler() {
var getamount = localStorage.getItem(\'amount\');
console.log(\'amount of posts to retrive \' + JSON.parse(getamount));
// Ajax call
$.ajax({
type: \'GET\',
data: {
posts: getamount,
page: page,
category: \'work\'
},
dataType: \'html\',
url: \'http://dev.xxx.se/wp-content/themes/xxx/batch.php\',
beforeSend: function() {
_setHeader;
if( page != 1 ) {
console.log(\'Loading\');
// Show the preloader
$(\'body\').prepend(\'<div class="preloader"><span class="rotate"></span></div>\');
}
// If we reach the end we hide the show more button
if( page >= total ) {
$(\'.load\').hide();
}
},
success: function(data) {
console.log(page);
var scroll = ($(\'.thumb\').height() * posts);
// If thumbs exist append them
if( data.length ) {
// Append the data
$(\'#batch\').append(data);
// Remove the crappy width and height attrs from the image * Generated by WP *
$(\'img\').removeAttr(\'height\').removeAttr(\'width\');
// Animate each new object in a nice way
(function _showitem() {
$(\'#batch .thumb:hidden:first\').addClass(\'show\', 80, _showitem);
// On the last request do load any more
loading = false;
})();
// Remove the preloader
$(\'.preloader\').fadeOut(200, function() {
$(\'.preloader\').remove();
});
}
// return false;
},
complete: function() {
// Delete storage
localStorage.clear();
// Update the scroller to match the updated content length
if (scroller)
setTimeout("scroller.refresh()", 300);
// Initalize the load more button
_clickhandler();
},
error: function() {
console.log(\'No page found\');
}
});
}
和我的加载更多按钮功能 $(\'.load\').on(\'click\', function(event) {
event.preventDefault();
// Delete storage
localStorage.clear();
if(!loading) {
loading = true;
// Increase our pagenumber per click
page++;
count++;
// Remove preloader
$(\'.preloader\').remove();
setTimeout(function() {
$(\'#batch\').css({
\'-webkit-transform\' : \'translateY(-\' + ($(\'#batch li\').outerHeight() * count) + \'px)\'
});
}, 30);
// Clear storage and set a new
localStorage.setItem(\'amount\', JSON.stringify(amount.medium));
var getamount = localStorage.getItem(\'amount\');
// Send the request to the handler
_batchhandler(page);
}
});
一切似乎都很好,前10篇(1-10篇)帖子按照应该的方式加载,但第一次单击“加载更多”,我们会得到接下来的5个结果,但结果是第一次加载的帖子(5-10篇)。如果我们再次单击“加载更多”,我们会得到正确的结果