3

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.

  • 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 May 30 '14 at 22:54
  • 1
    @boscho As this plugin is "future core", we (moderators) have (discussed and) decided that this plugin is on topic. – kaiser May 31 '14 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 May 31 '14 at 3:56
  • @RachelBaker Oh. Thanks! The OP will have to change the plugin to keep it on topic then. – kaiser May 31 '14 at 11:54
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;
}
-1

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

});

  • 4
    Please add a proper explanation to your code – Pieter Goosen Aug 27 '15 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.