我有一个带有page参数的自定义ajax调用,用于接收home.php模板上的帖子。它可以工作,但是当我将page参数更改为另一个参数时,可以这样说:
http://localhost/<my-wordpress-folder>/wp-admin/admin-ajax.php?action=getPaginatedProjects&page=2
无论参数多少,它总是返回首页结果。
我正在使用Sage 9入门主题。
Home.php控制器
<?php
namespace App\Controllers;
use Sober\Controller\Controller;
use WP_Query;
class Home extends Controller
{
public function __construct() {
add_action( 'wp_ajax_getPaginatedProjects', function() {
$response = $this->getPaginatedProjects();
echo $response;
wp_die();
});
add_action( 'wp_ajax_nopriv_getPaginatedProjects', function() {
$response = $this->getPaginatedProjects();
echo $response;
wp_die();
});
}
public function projectsQuery($page = 1) {
$args = [
'post_type' => 'post',
'posts_per_page' => 5,
'post_status' => ['publish'],
'paged' => $page
];
$projects = new WP_Query($args);
return $projects;
}
protected function getPaginatedProjects() {
$response = '';
$page = strval($_POST['page']);
$query = $this->projectsQuery($page);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$response .= \App\template(locate_template('views/partials/single.blade.php'));
}
}
wp_reset_postdata();
$response = json_encode($response);
return $response;
}
}
答案 0 :(得分:0)
您要求一页包含5个帖子
尝试$ page-= 1;