我正在使用WP REST API从具有自定义分类的自定义post返回数据。这是我的代码-
add_action( 'rest_api_init', 'register_book_type' );
function register_book_type() {
register_rest_field( 'lesson',
'book_type',
array(
'get_callback' => 'get_book_type',
'update_callback' => null,
'schema' => null,
)
);
}
function get_book_type( $object, $field_name, $request ) {
$name = [];
$object = get_terms(array(
'taxonomy' => 'book_type'
));
foreach ($object as $term) :
array_push($name, $term->name.' ');
endforeach;
return implode('/ ', $name);
}
这将返回所有的分类法名称,但我需要它只返回适用于特定帖子的分类法名称。
我如何才能将其更改为只显示特定帖子的分类法名称?
发布于 2021-09-25 16:16:20
我将$object = get_term更改为
$object = get_the_terms( $post->ID, 'book_type' );
这是对特定帖子的拉动
https://stackoverflow.com/questions/69320798
复制相似问题