<?php
include("../wp-config.php");
// set header for json mime type
header('Content-type: application/json;');
//$output = array('categories' => array());
$categories=$_GET['name'];
$post=get_posts('category_name='.$categories.'&posts_per_page=-1');
$category_query_args = array(
//'cat_name' => $categories,
'post' => $post
);
//print_r($category_query_args);
$category_query = new WP_Query($category_query_args);
if ( $category_query->have_posts() )
{
while ($category_query->have_posts())
{
$jsonpost=$category_query->the_post();
$jsonpost['title']= get_the_title();
$jsonpost['date'] = get_the_date();
}
}
$output['category_details'][] = $jsonpost;
/*======== PRODUCT COLLECTION OUTPUT AS JSON FORMAT ======*/
echo (json_encode($output));
?>
我想获取某个类别中的所有帖子。该类别是从网址获取的。在上面的代码中,我在该类别中获得了一篇帖子。想要获得所有帖子。需要帮助!
答案 0 :(得分:0)
你有“$ output ['category_details'] [] = $ jsonpost;”不合时宜的舅舅,¿这就是这个?
答案 1 :(得分:0)
也许问题是你在每次迭代时初始化$ jsonpost数组:
$jsonpost=$category_query->the_post();
因此,在循环结束时,您的数组只有一个条目。
您可以使用一个数组进行提取,使用其他数组进行推送
答案 2 :(得分:0)
<?php
include("../wp-config.php");
// set header for json mime type
header('Content-type: application/json;');
//$output = array('categories' => array());
$categories=$_GET['name'];
$args = array('numberposts=' => -1, 'category_name' => $categories);
$myposts = get_posts($args);
foreach( $myposts as $post )
{
$id=$post->ID;
$image=wp_get_attachment_url( get_post_thumbnail_id($id, 'thumbnail') );
$date=get_the_date();
$content=apply_filters('the_content', $post->post_content);
$cat_output['posts'][] = array(
'name' => get_the_title($post->ID),
'image' => $image,
'date' => $date,
'Content' => $content
);
}
$output['categories'][] = $cat_output;
header("Content-type: application/json");
die(json_encode($output));
?>
通过使用上面的代码,我得到了我想要的结果。但是当我使用 WP_Query 时,我不知道为什么我得到了结果。如果有人都知道了,请发表您的建议。 谢谢!
答案 3 :(得分:0)
试试这个(刚刚更改了NEW / EDIT行)
<?php
include("../wp-config.php");
// set header for json mime type
header('Content-type: application/json;');
//$output = array('categories' => array());
$categories=$_GET['name'];
$post=get_posts('category_name='.$categories.'&posts_per_page=-1');
$category_query_args = array(
//'cat_name' => $categories,
'post' => $post
);
//print_r($category_query_args);
$category_query = new WP_Query($category_query_args);
if ( $category_query->have_posts() )
{
$jsonall = array(); // NEW
while ($category_query->have_posts())
{
$jsonpost=$category_query->the_post();
$jsonpost['title']= get_the_title();
$jsonpost['date'] = get_the_date();
$jsonall[] = $jsonpost; // NEW
}
}
$output['category_details'][] = $jsonall; // EDIT
/*======== PRODUCT COLLECTION OUTPUT AS JSON FORMAT ======*/
echo (json_encode($output));
?>
:)
答案 4 :(得分:0)
<?php
include("../wp-config.php");
// set header for json mime type
header('Content-type: application/json;');
//$output = array('categories' => array());
$categories=$_GET['name'];
//echo $categories;
// $post=array('category_name='.$categories.'&posts_per_page=-1');
$post = array('category_name' => $categories);
//print_r($category_query_args);
$category_query = new WP_Query($post);
while ($category_query->have_posts())
{
$jsonpost=$category_query->the_post();
$jsonpost['title']= get_the_title();
$jsonpost['date'] = get_the_date();
$output['category_details'][] = $jsonpost;
}
/*======== PRODUCT COLLECTION OUTPUT AS JSON FORMAT ======*/
echo (json_encode($output));
?>
我使用 WP_Query 本身得到了结果:)我刚删除了 get_posts(),并在数组中传递了 categoryname ( $ post )。这将传递给 WP_Query($ post)。如 @EnriqueMuñozRodas说,“$ output ['category_details'] [] = $ jsonpost;”被移到循环。
谢谢@EnriqueMuñozRodas!!