[英]Show WP category id in json
I made a page that shows some data in json format from a Wordpress database. 我创建了一个页面,从Wordpress数据库中以json格式显示一些数据。 Now I want show the category id of posts, but it shows all the ids before the object "ads"
现在我想要显示帖子的类别ID,但它显示对象"ads"
之前的所有ID
My source: 我的来源:
<?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,
'thumbnail' => (has_post_thumbnail() ? get_the_post_thumbnail_url() : ''),
'cats' => the_category_ID(),
);
endwhile; endif;
echo json_encode(array('ads' => $posts));
?>
It shows IDs before because of this line 'cats' => the_category_ID(),
由于此行'cats' => the_category_ID(),
它显示之前的ID 'cats' => the_category_ID(),
What does this function do? 这个功能有什么作用?
This function actually echos the category ID ( as it's argument is true by default ). 该函数实际上回应了类别ID(因为它的参数默认为true)。
How to fix? 怎么修?
Pass false
inside the_category_ID
like this the_category_ID( false )
在the_category_ID
传递false
,如下所示the_category_ID( false )
Corrected code 更正了代码
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));
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;
use ob clean 用ob清洁
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.