简体   繁体   中英

Add Custom Endpoint in WordPress Rest Api

I want to add custom endpoint in WordPress Rest API.I am able to fetch the post id,title,content,slug,categories and featured_image through this code by creating simple plugin.I am obtained category id in the Code.I want the category name i tried to do this by get_cat_name But did not understand it. How can i get category name,author name and author profile image through custom endpoint. I am also a beginner in wordpress. I refer to the documentation but did not understand how to do it. Write it as in 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',
    ) );

}); 

You are getting category id then you can get category name using it' id like:

get_the_category_by_ID( $cat_ID )

To get the author name and image you can do as following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM