How to get featured post title & image using JSON API?

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

I tried using this:

example.com/?json=the_post_thumbnailscount=3

But instead of recent posts, I get featured posts.

Topic featured-post json post-thumbnails api Wordpress

Category Web


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

});

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.