Retrieving a JSON feed of my posts and displaying thumbnails

Try something like this: $posts = $query->get_posts(); foreach($posts as $p){ $thumb_id = get_post_thumbnail_id($p->ID); $src = wp_get_attachment_image_src($thumb_id, ‘image_size’); $url = $src ? $src[0] : false; $p->image_url = $url; } $json = json_encode($posts); Now you can echo out $json into your JS var. You will find the image url string in the key image_url of your json … Read more

Retrieve list of taxonomies in json

Your problem is that your trying to access taxonomy data before it is registered. This doesn’t work: add_action(‘init’, ‘json_handler’); function json_handler(){ $categories = get_terms( ‘my_cat’, ‘orderby=count&hide_empty=0’ ); if( ! is_wp_error( $categories ) ) { // encode the $categories array as json print_r( json_encode( $categories ) ); } } add_action(‘init’, ‘create_stores_nonhierarchical_taxonomy’); function create_stores_nonhierarchical_taxonomy() { // Labels … Read more

Can’t add classes using jQuery from a JSON string with get_body_class()

get_body_class() is going to return an array of classes, which is then (per code not published in your question) JSON encoded into that comma separated string. While you can manipulate that in JavaScript, the easiest thing to do is implode the string before encoding: $c = get_body_class(‘project’); $c = implode($c,’ ‘); You should have a … Read more

Call Ajax URL in Plugin

Put below code in _construct() function and change action name to get_product_serial_callback :- add_action( ‘wp_ajax_get_product_serial_callback’, array($this,’get_product_serial_callback’) ); add_action( ‘wp_ajax_nopriv_get_product_serial_callback’, array($this,’get_product_serial_callback’ ));

WP Rest API and json_decode()

wp_remote_get return array of results containing keys body, headers, response etc depending on arguments. And json_decode only accept JSON string. So correct your code in this way //return if not an error if ( ! is_wp_error( $response ) ) { //decode and return return json_decode( wp_remote_retrieve_body( $response ) ); } Ref: Return Values of wp_remote_get … Read more