我正在建立一个网站,使用户能够使用wordpress发表文章(我建立了自己的主题)。并且在主页中,用户将按用户查看其后的帖子(例如Twitter时间轴)。我使用基于jQuery的无限滚动来代替分页。
一切都很好,并且如果我以admin身份登录也可以使用,问题是当我以其他权限(例如作者)登录时。该页面将仅加载第一页。并且不会加载另一页面。
这是我在function.php中的代码:
//-----------------Infinite Scroll-------------------------------
/*
* initial posts dispaly
*/
function script_load_more($args = array()) {
//initial posts load
echo '<div id="ajax-primary" class="content-area">';
echo '<div id="ajax-content" class="content-area">';
ajax_script_load_more($args);
echo '</div>';
echo '<center><a href="#" id="loadmore" data-page="1" data-url="'.admin_url("admin-ajax.php").'" ></a></center>';
echo '</div>';
}
/*
* create short code.
*/
add_shortcode('ajax_posts','script_load_more');
/*
* load more script call back
*/
function ajax_script_load_more($args) {
//init ajax
$ajax = false;
//check ajax call or not
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$ajax = true;
}
global $wpdb;
$cekpost = $wpdb->get_results ( "SELECT post_author FROM ".$wpdb->prefix."posts WHERE post_author = ".get_current_user_id()." AND post_status='publish'");
$result = $wpdb->get_results ( "SELECT follow FROM ".$wpdb->prefix."follow WHERE user = ".get_current_user_id());
if (sizeof($cekpost)>0 OR sizeof($result)>0){
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$jumlah=count($result);;
$i=0;
$following = get_current_user_id();
foreach ( $result as $hasil )
{
$i++;
//if ($i<$jumlah){
$following=$following.",".$hasil->follow;
//$following=$following.",";
//}
}
//echo $following;
if ($following!=null){
echo "inside following".get_current_user_id();
//args
$args = array(
'post_type' => 'post',//'author' => $following,'post_status' => 'publish','posts_per_page' =>$num,'paged'=>$paged
);
//query
$query = new WP_Query($args);
//var_dump($query);
//check
if ($query->have_posts()):
//loop articles
while ($query->have_posts()): $query->the_post();
include 'ajax-content.php';
//reset post data
wp_reset_postdata();
endwhile;
endif;
}
//check ajax call
if($ajax) die();
} else {
include_once "postuser.php";
}
}