• Hi,
    In the API output, is it possible to display the source URL of the featured image for the post? I’m attempting to use the API and Handlebars JS templating to display the related posts, but the featured images aren’t included.

    Thanks

Viewing 1 replies (of 1 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi jeffreybetts

    You can add the featured image url to the related posts with a filter.
    Try it with this in your (child) theme’s functions.php file.

    
    add_filter( 'related_posts_by_taxonomy', 'add_thumbnail_wp_rest_api_posts', 10, 4 );
    
    function add_thumbnail_wp_rest_api_posts( $related, $post_id, $taxonomies, $args ) {
    	if ( 'wp_rest_api' !== $args['type'] ) {
    		return $related;
    	}
    
    	foreach ( $related as $key => $post ) {
    		if ( is_object( $post ) ) {
    			$related[ $key ]->post_thumbnail = get_the_post_thumbnail_url( $post );
    		}
    	}
    
    	return $related;
    }
    

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

Viewing 1 replies (of 1 total)
  • The topic ‘Show Featured Image URL in API Result’ is closed to new replies.