我目前正在建立一个WordPress网站。在这个站点中有作者页面,显示前十篇文章,还有一个加载更多按钮,通过Ajax调用获取下十篇文章。特别是对一位作者来说,这是在复制列表上的最后一篇文章,我能看到的唯一原因是,他们在前十篇文章中有一篇草稿,这篇文章是按偏移量计算的。这在其他任何地方都能完美地工作,所以我无法找出问题所在。
Functions.php
if (!function_exists(\'bookreview_moreajax\')) {
function bookreview_moreajax(){
$ppp = (isset($_POST[\'ppp\'])) ? $_POST[\'ppp\'] : 3;
$cat = (isset($_POST[\'cat\'])) ? $_POST[\'cat\'] : 0;
$auth = (isset($_POST[\'auth\'])) ? $_POST[\'auth\'] : 0;
$offset = (isset($_POST[\'offset\'])) ? $_POST[\'offset\'] : 0;
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $ppp,
\'cat\' => $cat,
\'offset\' => $offset,
\'author_name\' => $auth,
);
$loop = new WP_Query($args);
$out = \'\';
if ($loop -> have_posts()) :
while ($loop -> have_posts()) :
$loop -> the_post();
$out = get_template_part( \'template-parts/content\', \'list\' );
endwhile;
endif;
wp_reset_postdata();
wp_die($out);
}
}
Ajax.js
(function ($) {
"use strict";
/*jslint browser: true*/
/*global $,console,ajaxpagination*/
//AJAX MORE POSTS
//global variables
var $content = $(\'.ajax_posts\'),
$pageloader = $(\'#more_posts\'),
$catloader = $(\'.categories_EBR .radio-buttons li.cat-item\'),
offset = 0,
auth = \'\',
cat = \'\',
loading = false;
//show load more button and hide pagination
if (($pageloader).length) {
$(\'.navigation\').css(\'display\', \'none\');
$pageloader.css(\'display\', \'block\');
}
function get_ajax_posts(ppp, method, $loader) {
loading = true;
//set offset depending on method
if (method === \'append\') {
offset = $(\'#main\').find(\'.type-post\').length;
} else {
$content.css({ opacity: 0.5 });
offset = 0;
}
//start ajax call
$.ajax({
type: \'POST\',
dataType: \'html\',
url: ajaxpagination.ajaxurl,
data: {
\'cat\': cat,
\'ppp\': ppp,
\'offset\': offset,
\'auth\': auth,
\'action\': \'bookreview_moreajax\'
},
beforeSend : function () {
$(\'body\').addClass(\'ajax-loading\');
},
success: function (data) {
loading = false;
var $data = $(data);
if ($data.length) {
var $newElements = $data.css({ opacity: 0 });
//update or replace depending on method
if (method === \'append\') {
$content.append($newElements);
} else if (method === \'replace\') {
$content.html($data);
} else {
console.log(\'Unknown method: \' + method);
return false;
}
//update Load More button with correct category
$pageloader.data(\'category\', cat);
$newElements.animate({ opacity: 1 }, 200);
$pageloader.css({ opacity: 1 }).html(ajaxpagination.loadmore);
} else {
$pageloader.css({ opacity: 1 }).addClass(\'no_more_posts\').html(ajaxpagination.noposts);
}
$content.css({ opacity: 1});
$loader.removeClass(\'ajax-loader\');
$(\'body\').removeClass(\'ajax-loading\');
},
error : function (jqXHR, textStatus, errorThrown) {
loading = false;
$pageloader.html($.parseJSON(jqXHR.responseText) + \' :: \' + textStatus + \' :: \' + errorThrown);
console.log(jqXHR);
}
});
offset += ppp;
return false;
}
function get_more(event) {
if (loading || $pageloader.hasClass(\'no_more_posts\')) {
console.log("Stahp!");
return;
}
var ppp = 6,
method = \'append\',
$loader = $(event.currentTarget);
if ($pageloader.attr(\'data-category\')) {
cat = $pageloader.data(\'category\');
} else if ($pageloader.attr(\'data-author\')) {
auth = $pageloader.data(\'author\');
}
$loader.addClass(\'ajax-loader\');
get_ajax_posts(ppp, method, $loader);
}
function get_cat(event) {
event.preventDefault();
if (loading) {
console.log("Stahp!");
return;
}
var $loader = $(event.currentTarget),
$cats = $(\'.cat-item\'),
ppp = 10,
method = \'replace\';
if (!($loader.hasClass(\'current-cat\'))) {
$cats.removeClass(\'current-cat\');
cat = $loader.attr(\'class\').match(/\\d+/)[0];
$loader.addClass(\'current-cat\');
} else {
cat = $(\'body\').attr(\'class\').match(/\\d+/)[0];
$cats.removeClass(\'current-cat\');
}
$pageloader.removeClass(\'no_more_posts\');
get_ajax_posts(ppp, method, $loader);
}
//triggers
$pageloader.on(\'click\', get_more);
$catloader.on(\'click\', get_cat);
}(jQuery));
似乎在接下来的十个帖子中,偏移量(10)似乎算上了草稿帖子,因此落后了一个,导致了一个重复帖子。我是否遗漏了一些明显的东西?
编辑-当我向查询参数添加post\\u status=>“published”时,它跳过10篇文章(与offset的值相同)。当我强制偏移为0时,它只会重复前10个。如果我删除帖子状态,它将继续统计草稿帖子。
EDIT2-原来是因为我有大量的私人帖子。通过将此添加到我的函数中。php问题已修复:
// check if user is editor or higher to allow viewing private posts
if( current_user_can(\'editor\') || current_user_can(\'administrator\') ) {
$post_status = array (\'publish\', \'private\');
} else {
$post_status = array (\'publish\');
}
$args = array(
\'post_status\' => $post_status,
\'posts_per_page\' => $ppp,
\'cat\' => $cat,
\'offset\' => $offset,
\'author_name\' => $auth,
);
仍然想知道为什么这不会自动发生(默认情况下是这样),这样做似乎有点混乱。关于如何更干净地执行此操作,有什么建议吗?
SO网友:CodeMascot
posts查询有一个参数是post_status
. 其值如下所示-
$args[\'post_status\'] => array( //(string / array) - use post status. Retrieves posts by Post Status, default value i\'publish\'.
\'publish\', // - a published post or page.
\'pending\', // - post is pending review.
\'draft\', // - a post in draft status.
\'auto-draft\', // - a newly created post, with no content.
\'future\', // - a post to publish in the future.
\'private\', // - not visible to users who are not logged in.
\'inherit\', // - a revision. see get_children.
\'trash\' // - post is in trashbin (available with Version 2.9).
),
因此,您可以使用此
post_status
. 只需添加
post_status
到您的
$args
你需要从下面的柱子上拉出来-
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $ppp,
\'cat\' => $cat,
\'offset\' => $offset,
\'author_name\' => $auth,
\'post_status\' => array( //(string / array) - use post status. Retrieves posts by Post Status, default value i\'publish\'.
\'publish\', // - a published post or page.
\'pending\', // - post is pending review.
// \'draft\', // - a post in draft status.
// \'auto-draft\', // - a newly created post, with no content.
\'future\', // - a post to publish in the future.
\'private\', // - not visible to users who are not logged in.
\'inherit\', // - a revision. see get_children.
\'trash\' // - post is in trashbin (available with Version 2.9).
),
);
希望这有帮助。