在json中显示WP类别ID

时间:2016-07-17 08:32:40

标签: php json wordpress

我创建了一个页面,显示来自Wordpress数据库的json格式的一些数据。现在我希望显示帖子的类别ID,但它会显示对象UIControllEventEditingChanged

之前的所有ID

enter image description here

我的来源:

"ads"

2 个答案:

答案 0 :(得分:0)

由于此行,它显示之前的ID AppComponent

这个功能有什么作用?

此函数实际上回应了类别ID(因为它的参数默认为true)。

如何解决?

'cats' => the_category_ID(), false内传递the_category_IDthe_category_ID( false )

更正后的代码

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,

        'thumbnail' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
        'cats' => the_category_ID( false ),

    );
endwhile; endif;
echo json_encode(array('ads' => $posts));

答案 1 :(得分:0)

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,

        'thumbnail' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
        'cats' => the_category_ID(),
    );
    ob_clean();
endwhile; endif;

使用ob clean