好时间,
我正在使用AJAX在wordpress中加载更多帖子。 但是我用wp_query和loop不能获得超过3个帖子。
我的AJAX代码:
$('#fetchArticle').click(function (html){
var offset = 0;
let current_page = document.querySelector(".posts_list").dataset.page;
let max_page = document.querySelector(".posts_list").dataset.max;
offset = offset + 3;
$(this).text('wait...');
$(this).prop('disabled', true);
$btnLoad = $(this);
$val = $btnLoad.val();
$.ajax({
url: 'moreArticle.php',
type: 'POST',
dataType: 'html',
data : {'html' : 'html'},
cache: false,
success : function (response){
$('#Articles').append(response);
$btnLoad.prop('disabled', false);
$btnLoad.text('load more ? ');
},
error: function ($e){
alert('server connection is feild....!');
$btnLoad.prop('disabled', false);
$btnLoad.text('try agian !');
console.log($e);
}
})
})
我的php代码是:
<?php
session_start();
require( 'wp-load.php' );
$args = array(
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => '3',
'offset' => '3'
);
$my_post = new WP_Query( $args );
$html = '';
$outPut = array();
while ($my_post->have_posts()) {
$my_post->the_post();
$pic = get_the_post_thumbnail_url();
$title = get_the_title();
$categories = get_the_category();
$cat = $categories[0]->name;
function excerpt2($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt). " ...";
return $excerpt;
}
$ex = excerpt2(10);
$author = get_the_author();
$time = get_the_time();
$link = get_the_permalink();
$html .= '<div class="col-md-4"><div class="card card-blog"><div class="card-image"><a href="#pablo"><img class="img" alt="'.get_the_title().'" src="'.$pic.'" /></a></div>';
$html .= '<div class="card-content"><h6 class="category text-danger"><i class="material-icons"></i>'.$cat.'</h6><h4 class="card-title"><a href="#pablo">'.$title.'</a></h4>';
$html .= '<div class="footer text-center"><div class="author"><a href="#pablo"><span>'.$author.'</span><img src="" alt="..." class="avatar img-raised"></a></div>';
$html .= '<div class="stats"><i class="material-icons">schedule</i>'.$time.'</div>';
$html .= '<a href="'.$link.'" class="btn btn-primary btn-round"><i class="material-icons">format_align_right</i> ادامه مطلب</a></div></div></div>';
wp_reset_postdata();
array_push($outPut , $html);
}
echo $outPut;
但是每次我请求时,浏览器都会向我发出以下提示:alert('服务器连接是字段....!');
我真的很困惑! 谢谢您的帮助。