2016-08-20 67 views
-1

有人爲我做了這段代碼,以json格式加載文章,但它只加載了10篇最新文章。我如何將其更改爲加載所有帖子?爲什麼這段代碼只加載10條最新帖子?

我試過?page = 2但它不起作用。

json url

代碼

<?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

頁取得參數沒有傳遞到WordPress的查詢:'$循環=新WP_Query(陣列(「post_status」 =>'publish','post_type'=>'post'));' – jedifans

+0

那我該怎麼辦?我希望在一個頁面中顯示所有帖子 – hesam

回答

1

您應該採取一看docs。有一個預先設定的posts_per_page PARAM將被設置爲-1如果你希望所有的帖子:

$loop = new WP_Query(
    array('post_status' => 'publish', 'post_type' => 'post', 'posts_per_page' => -1) 
);