首页 > 解决方案 > 我不能加载多个帖子

问题描述

美好时光,

我正在使用 AJAX 在 wordpress 中加载更多帖子。但是我不能用 wp_query 和循环获得超过 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('server connection is field....!');

我真的很困惑!谢谢您的帮助。

标签: phpjqueryajaxwordpress

解决方案


推荐阅读