在 WordPress Rest 中添加自定义端点 Api
Add Custom Endpoint in WordPress Rest Api
我想在 WordPress Rest 中添加自定义端点 API.I 我能够通过创建简单的代码通过此代码获取 post id、标题、内容、slug、类别和 featured_image插入。我在 Code.I 中获得类别 ID 想要类别名称 我试图通过 get_cat_name
执行此操作但不理解。如何通过自定义端点获取类别名称、作者姓名和作者个人资料图像。我也是wordpress的初学者。我参考了文档,但不明白该怎么做。按照w_posts function for category name
的方式编写
$data[$i]['catname']= get_cat_name($data[$i]['categories']);
<?php
/** Plugin Name : Custom API
* Plugin URI : https://google.com
* Decription : Crushing It
* Version : 1.0
* Author: Shahryar
* Author URI: https://google.com
*/
/** Plugin Name: Custom API... */
function w_posts(){
$args = [
'numberposts'=> 99999,
'post_type' => 'post'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['content'] = $post->post_content;
$data[$i]['slug'] = $post->post_name;
$data[$i]['categories'] = $post->post_category;
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
$i++;
}
return $data;
}
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function my_awesome_function( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function w_post( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['slug'] = $post[0]->post_name;
$data['categories'] = $post[0]->post_category;
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
add_action('rest_api_init',function(){
register_rest_route('w/v1','posts',[
'methods'=> 'GET',
'callback'=>'w_posts',
]);
register_rest_route( 'w/v1', 'posts/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'w_post',
) );
});
您正在获取类别 ID,然后您可以使用它的 ID 获取类别名称,例如:
get_the_category_by_ID( $cat_ID )
要获取作者姓名和图片,您可以按照以下步骤操作:
$author_id=$post->post_author; //get author id
//get author image URL by author id
$author_img_url = the_author_meta( 'avatar' , $author_id );
//get author name by author id
the_author_meta( 'user_nicename' , $author_id );
我想在 WordPress Rest 中添加自定义端点 API.I 我能够通过创建简单的代码通过此代码获取 post id、标题、内容、slug、类别和 featured_image插入。我在 Code.I 中获得类别 ID 想要类别名称 我试图通过 get_cat_name
执行此操作但不理解。如何通过自定义端点获取类别名称、作者姓名和作者个人资料图像。我也是wordpress的初学者。我参考了文档,但不明白该怎么做。按照w_posts function for category name
$data[$i]['catname']= get_cat_name($data[$i]['categories']);
<?php
/** Plugin Name : Custom API
* Plugin URI : https://google.com
* Decription : Crushing It
* Version : 1.0
* Author: Shahryar
* Author URI: https://google.com
*/
/** Plugin Name: Custom API... */
function w_posts(){
$args = [
'numberposts'=> 99999,
'post_type' => 'post'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['content'] = $post->post_content;
$data[$i]['slug'] = $post->post_name;
$data[$i]['categories'] = $post->post_category;
$data[$i]['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post->ID, 'thumbnail');
$data[$i]['featured_image']['medium'] = get_the_post_thumbnail_url($post->ID, 'medium');
$data[$i]['featured_image']['large'] = get_the_post_thumbnail_url($post->ID, 'large');
$i++;
}
return $data;
}
function my_awesome_func( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function my_awesome_function( $data ) {
$posts = get_posts( array(
'author' => $data['id'],
) );
if ( empty( $posts ) ) {
return null;
}
return $posts[0]->post_title;
}
function w_post( $slug ) {
$args = [
'name' => $slug['slug'],
'post_type' => 'post'
];
$post = get_posts($args);
$data['id'] = $post[0]->ID;
$data['title'] = $post[0]->post_title;
$data['content'] = $post[0]->post_content;
$data['slug'] = $post[0]->post_name;
$data['categories'] = $post[0]->post_category;
$data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
$data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
$data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');
return $data;
}
add_action('rest_api_init',function(){
register_rest_route('w/v1','posts',[
'methods'=> 'GET',
'callback'=>'w_posts',
]);
register_rest_route( 'w/v1', 'posts/(?P<slug>[a-zA-Z0-9-]+)', array(
'methods' => 'GET',
'callback' => 'w_post',
) );
});
您正在获取类别 ID,然后您可以使用它的 ID 获取类别名称,例如:
get_the_category_by_ID( $cat_ID )
要获取作者姓名和图片,您可以按照以下步骤操作:
$author_id=$post->post_author; //get author id
//get author image URL by author id
$author_img_url = the_author_meta( 'avatar' , $author_id );
//get author name by author id
the_author_meta( 'user_nicename' , $author_id );