我为我的自定义帖子类型制作自定义端点。一件事是分类 ID。我需要通过帖子 ID 获取分类 ID。get_terms( 'gallery_tax', $post->ID )给我包含所有分类对象的数组
function wl_posts() {
$args = [
'numberposts' => 9999,
'post_type' => 'gallery',
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach ($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['fimg_url'] = get_the_post_thumbnail_url($post->ID, 'large');
$data[$i]['proizvoditel'] = get_field('proizvoditel', $post->ID);
$data[$i]['tip'] = get_field('tip', $post->ID);
$data[$i]['razmer'] = get_field('razmer', $post->ID);
$data[$i]['forma'] = get_field('forma', $post->ID);
$data[$i]['rost'] = get_field('rost', $post->ID);
$data[$i]['ves'] = get_field('ves', $post->ID);
$data[$i]['ohvat'] = get_field('ohvat', $post->ID);
$data[$i]['vozrast'] = get_field('vozrast', $post->ID);
$data[$i]['galereya'] = get_field('galereya', $post->ID);
$data[$i]['taxonomy'] = get_terms( 'gallery_tax', $post->ID );
$i++;
}
return $data;
}
add_action('rest_api_init', function() {
register_rest_route('wl/v1', 'gallery', [
'methods' => 'GET',
'callback' => 'wl_posts',
]);
});
繁华开满天机