在WordPress Rest Api中添加自定义端点

问题描述 投票:0回答:1

我想在WordPress Rest API中添加自定义终结点。通过创建简单的插件,我可以通过此代码获取帖子ID,标题,内容,条目,类别和featured_image。我在Code中获得了类别ID。我想要通过get_cat_name尝试执行的类别名称,但不理解。我如何通过自定义端点获取类别名称,作者名称和作者个人资料图像。我也是wordpress的初学者。我参考了文档,但不知道如何去做。将其写成w_posts函数中的类别名称

$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',
    ) );

}); 
php wordpress endpoint wordpress-rest-api
1个回答
0
投票

您正在获取类别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 );
© www.soinside.com 2019 - 2024. All rights reserved.