admin管理员组

文章数量:1122832

Is there any possibility to get featured post title and image using the JSON API.

I tried using this:

example/?json=the_post_thumbnails&count=3

But instead of recent posts, I get featured posts.

Is there any possibility to get featured post title and image using the JSON API.

I tried using this:

example.com/?json=the_post_thumbnails&count=3

But instead of recent posts, I get featured posts.

Share Improve this question edited May 31, 2014 at 1:10 kaiser 50.8k27 gold badges150 silver badges244 bronze badges asked May 30, 2014 at 22:37 warzone_fzwarzone_fz 1392 silver badges4 bronze badges 4
  • 1 Hello warzone_fz - welcome to the WordPress Development community! Questions regarding 3rd-party plugins and themes are considered off-topic for our community as it's rare that many if any members of our community has experience with such items - as such these questions tend to remain unanswered. Your question would be best asked in the official support channels for your plugin(s). Please read the on-topic and How to Ask sections of our help center for more information regarding what questions are a good fit for our community. – bosco Commented May 30, 2014 at 22:54
  • 1 @boscho As this plugin is "future core", we (moderators) have (discussed and) decided that this plugin is on topic. – kaiser Commented May 31, 2014 at 1:09
  • 2 @kaiser This question is related to another JSON API plugin, not the one slated for inclusion with WP core. It is related to: wordpress.org/plugins/json-api – Rachel Baker Commented May 31, 2014 at 3:56
  • @RachelBaker Oh. Thanks! The OP will have to change the plugin to keep it on topic then. – kaiser Commented May 31, 2014 at 11:54
Add a comment  | 

2 Answers 2

Reset to default 0

I made a shortcut to my image by adding it directly to the API response.


//Add in functions.php, this hook is for  my 'regions' post type
add_action( 'rest_api_init', 'create_api_posts_meta_field' );

function create_api_posts_meta_field() {
  register_rest_field( 'regions', 'group', array(
         'get_callback'    => 'get_post_meta_for_api',
         'schema'          => null,
      )
  );
}

//Use the post ID to query the image and add it to your payload. 


function get_post_meta_for_api( $object ) {
  $post_id = $object['id'];
  $post_meta = get_post_meta( $post_id );
  $post_image = get_post_thumbnail_id( $post_id );      
  $post_meta["group_image"] = wp_get_attachment_image_src($post_image)[0];


  //var_dump(wp_get_attachment_image($post_image)); die();
  //Different image codex return different values, narrow in on what you want

  return $post_meta;
}

Tty this one

jQuery.getJSON('/latest-post-as-json/', function(data) {

    jQuery('#externalBlock').append('<h1>'+data.post_title+'</h1>');

    jQuery('#externalBlock').append(data.post_content);



//etc

});

本文标签: How to get featured post title amp image using JSON API