7

I am a big fan of the JSON API plugin. The developer did a great job and I thank him very much. I am learning how to work with json and jquery and am trying to limit the information I get back from an ajax request. I have been able to use the include and custom field options successfully but amd falling a little short understanding 4.6. Attachment response object. Is there a way to limit the result to only provide thumb images? If so, can anyone provide an example of the syntax. I am not sure how to address these objects in the query string. Any help would be awesome. Let me know if I need to clarify anything.

I managed to narrow down my result with this request url: /?json=get_recent_posts&include=title,url,categories,thumbnail,custom_fields&custom_fields=field1'

plugin url: http://wordpress.org/extend/plugins/json-api/other_notes/ plugin author: http://profiles.wordpress.org/users/dphiffer/

Regards, Fellow WordPress Developer

1
  • 1
    You should really try to contact the plugin author directly. He will be far more qualified to support this plugin than we will be. Commented Oct 18, 2011 at 16:41

3 Answers 3

2

I think it really all depends on what the issue with limiting this information is. Do you not want this particular data to be exposed? Do you not want to iterate through so much data? Are you intending on passing this data through a $.getJSON() request? Are you passing this data to a PHP function to handle it?

You can create a new controller to handle this with your own specification, where you're limiting your JSON output. A good example of a third-party controller is here: wordpress json custom taxonomy problem.

Or if you want an approach that I sometimes have done, where you pass the JSON output to a PHP variable, decode it, filter specific data to a new array, and either use that array as is, o re-encode it back to JSON format. A better example of this (more so pseudo code than code for you to use, as it's cut, pasted, and reorganized, directly from one of my projects):

$json = bbtf_feed_cache( '/api/get_recent_posts/?count=-1&post_type=highline_gallery', 'artists_jsonp' );

if( is_array( $json ) && ! empty( $json ) ) {
    $object  = $json['posts'];
    $artists = array();

    foreach( $object as $item ) {
        $artists[] = array( 'label' => $item['title_plain'], 'value' => $item['title_plain'], 'slug' => $item['slug'], 'id' => $item['id'] );
    }

    $json = json_encode( $artists );    
}

Let me know if this helps...

2

in your themes function file add:

add_theme_support( 'post-thumbnails' );    
set_post_thumbnail_size( 50, 50, true ); // 50 pixels wide by 50 pixels tall, crop mode

..etc - http://codex.wordpress.org/Post_Thumbnails#Setting_a_Post_Thumbnail

Then in your theme index file add the following inside the has post loop:

// check if the post has a Post Thumbnail assigned to it.    
if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}

Now when you call your JSON an extra field called thumbnail should be displayed.

0

If you are looking to limit the attachments/images which take up a bulk of the feed when using multiple custom sizes you can use the filter provided by the plugin author to remove certain attachment sizes

add_filter('json_api_encode', 'my_encode_attachments');

function my_encode_attachments($response) {
  if (isset($response['posts'])) {
    foreach ($response['posts'] as $post) {
        foreach ($post->attachments as $attachment){
            unset($attachment->images['full']);
            unset($attachment->images['thumbnail']);
            unset($attachment->images['medium']);
            unset($attachment->images['large']);
            unset($attachment->images['bones-thumb-300']);
            unset($attachment->images['bones-thumb-600']);
            unset($attachment->images['post-thumbnail']);
            unset($attachment->images['vendor-thumb']);
            unset($attachment->images['0']);
        }
    }
  } 
  return $response;
}
1
  • 1
    Simply set the values in "Admin UI" » "Settings" » "Media" to 0 and they will get skipped on creation. Commented Oct 2, 2012 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.