已解决!!!我的一个朋友找到了路
function get_latest_posts_by_term($request) {
$parsed_args=[
\'numberposts\' => 20,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'post_type\' => \'post\',
\'tax_query\' => array(
array (
\'taxonomy\' => $request[\'slug\'],
\'field\' => \'slug\',
\'terms\' => $request->get_param(\'name\'),
)
),
];
$get_posts = new WP_Query;
$posts= $get_posts->query( $parsed_args );
//$posts = get_posts($args);
if (empty($posts)) {
return new WP_Error( \'empty_terms\', \'There are no posts to display\', array(\'status\' => 404) );
}
$response = new WP_REST_Response($posts);
$response->set_status(200);
$data2 = [];
$i = 0;
foreach($posts as $post) {
$data2[$i][\'id\'] = $post->ID;
$data2[$i][\'title\'] = $post->post_title;
$data2[$i][\'excerpt\'] = $post->post_excerpt;
$data2[$i][\'date\'] = $post->post_date;
$data2[$i][\'content\'] = $post->post_content;
$data2[$i][\'slug\'] = $post->post_name;
$i++;
}
return $data2;
}
add_action(\'rest_api_init\', function() {
register_rest_route( \'wl/v1\', \'posts/terms/(?P<slug>[a-zA-Z0-9-]+)\',array(
\'methods\' => \'GET\',
\'callback\' => \'get_latest_posts_by_term\'
));
});```