访问具有特定 post 类型的所有内容 (Wordpress)
Access to all content with specific post type (Wordpress)
我对 Wordpress + rest 有疑问 api,
我使用 register_rest_route
为我的 collection 添加新的 end-point,我还创建了一个名为 'article' 的插件,这个 post 类型的类型也是'article'。
现在我想像这样使用 GET 在我的新 end-point 中访问所有这些 post 类型:
function my_awesome_func( $post_id ) {
$x = get_post_type_object( 'article' );
return $x[$post_id];
}
add_action( 'rest_api_init', function () {
register_rest_route( 'Articles/v2', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
我知道你不会写$x[$post_id]
。我写它是为了说我想访问特定的文章 ID。我应该怎么办?
谢谢。
我终于解决了这个问题,我post它,它可能对其他人有用。
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2/Articles', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
function my_awesome_func( $data ) {
$args = array(
'post_type' => 'article',
'p' => $data['id'],
);
$query = new WP_Query( $args );
return $query->post;
}
我对 Wordpress + rest 有疑问 api,
我使用 register_rest_route
为我的 collection 添加新的 end-point,我还创建了一个名为 'article' 的插件,这个 post 类型的类型也是'article'。
现在我想像这样使用 GET 在我的新 end-point 中访问所有这些 post 类型:
function my_awesome_func( $post_id ) {
$x = get_post_type_object( 'article' );
return $x[$post_id];
}
add_action( 'rest_api_init', function () {
register_rest_route( 'Articles/v2', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
我知道你不会写$x[$post_id]
。我写它是为了说我想访问特定的文章 ID。我应该怎么办?
谢谢。
我终于解决了这个问题,我post它,它可能对其他人有用。
add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2/Articles', '/author/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'my_awesome_func',
) );
} );
function my_awesome_func( $data ) {
$args = array(
'post_type' => 'article',
'p' => $data['id'],
);
$query = new WP_Query( $args );
return $query->post;
}