有人为我编写了这个代码,以json格式加载帖子,但它只加载了10个最新帖子。如何更改它以加载所有帖子?
我试过?page = 2但是它没有用。
代码
<?php
header("Content-type: application/json");
include ('wp-load.php');
$loop = new WP_Query(array( 'post_status' => 'publish', 'post_type' => 'post'));
if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post();
$posts[] = array(
'id' => $post->ID,
'post_title' => $post->post_title,
'post_content' => $post->post_content,
'guid' => $post->guid,
'image' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
'cats' => the_category_ID( false ),
'post_date' => $post->post_date,
);
endwhile; endif;
echo json_encode($posts);
?>
答案 0 :(得分:1)
你应该看看docs。如果您想要所有帖子,那么预设posts_per_page
参数将被设置为-1
:
$loop = new WP_Query(
array( 'post_status' => 'publish', 'post_type' => 'post', 'posts_per_page' => -1 )
);