【问题标题】:How WP REST API custom endpoint could return custom post data?WP REST API 自定义端点如何返回自定义帖子数据?
【发布时间】:2018-03-15 14:41:00
【问题描述】:

TL;DR:如何选择 WP REST API 自定义端点响应的每一点信息?

长版

如果我想使用 WP REST API 构建自定义端点——从不同的帖子类型发送特定的帖子数据——按照Handbook 中的示例,我得到了这个:

function custom_endpoint ( $data ) {
    $posts = get_posts( array(
        'numberposts'   => -1,
        'post_type'     => array('event', 'post'),
    ) );

    if ( empty( $posts ) ) {
        return null;
    }

    return $posts;
}


add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v1', '/custom-endpoint/', array(
        'methods' => 'GET',
        'callback' => 'custom_endpoint',
    ) );
} );

但是 get_post() 函数不会返回一些非常有用的数据,如果您希望在页面中显示帖子(例如类别 ID、特色图片)。那么如何构建一个返回的自定义端点:

  • 帖子标题
  • 发布日期
  • 文章作者
  • 文章摘录
  • 发布内容
  • 发布特色图片(如Better Featured Images plugin
  • 帖子类别
  • 帖子类型
  • 发布链接
  • 其他有用信息

【问题讨论】:

    标签: php wordpress rest api endpoint


    【解决方案1】:

    基于@fsn 的答案,我得到了以下想法:获取 get_posts() 返回的对象,并使用其他 Wordpress 函数为其添加新的比例。

    function custom_endpoint ( $data ) {
    $posts = get_posts( array(
        'numberposts'   => -1,
        //Here we can get more than one post type. Useful to a home page.
        'post_type'     => array('event', 'post'), 
    ) );
    
    
    if ( empty( $posts ) ) {
        return null;
    }
    
    
    $args = array();    
    
    foreach ( $posts as $post ) {
    
     //Get informations that is not avaible in get_post() function and store it in variables.
       $category = get_the_category( $post->ID );
       $img_thumb = get_the_post_thumbnail_url( $post->ID, 'thumbnail' );       // Thumbnail (default 150px x 150px max)
       $img_medium = get_the_post_thumbnail_url( $post->ID, 'medium' );          // Medium resolution (default 300px x 300px max)
       $img_large = get_the_post_thumbnail_url( $post->ID, 'large' );           // Large resolution (default 640px x 640px max)
       $img_full = get_the_post_thumbnail_url( $post->ID, 'full' );            // Full resolution (original size uploaded)
    
     //Adds the informations to the post object.
       $post->category = $category; 
       $post->img_tumb = $img_thumb; 
       $post->img_medium = $img_medium; 
       $post->img_large = $img_large; 
       $post->img_full = $img_full;
    
       array_push($args, $post);
       }
    return $args;
    }
    
    add_action( 'rest_api_init', function () {
      register_rest_route( 'wp/v1', '/custom-endpoint/', array(
        'methods' => 'GET',
        'callback' => 'custom_endpoint',
    ) );
    

    });

    效果很好!

    感谢@fsn 的贡献。

    【讨论】:

      【解决方案2】:

      作为WP Codex states访问所有数据:

      访问所有帖子数据 一些帖子相关数据不可用 get_posts。

      您可以通过以下方式获得它们:

      $posts = get_posts( array(
              'numberposts'   => -1,
              'post_type'     => array('event', 'post'),
          ) );
      
      $response = [];
      foreach ( $posts as $post ) {
        $response[] = [
          'content' => $post->CONTENT.
           'title' =>  $post->TITLE,
            .....
        ]
      }
      
      return $response; (in a WP way of constructing json responses)
      

      【讨论】:

      • 如果目标是在 Wordpress 页面中显示它会很好,但我需要通过 WP Rest API 将其作为 json 发送乙>。但它给了我一个想法。谢谢@fsn
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多