WP Rest API + AngularJS : How to grab Featured Image for display on page?

前端 未结 4 1186
伪装坚强ぢ
伪装坚强ぢ 2021-01-18 03:44

I am accessing Wordpress data through an HTTP REST API plugin (this wordpress plugin: http://v2.wp-api.org/). I know how to grab my post title, but how do I display the feat

4条回答
  •  粉色の甜心
    2021-01-18 04:14

    To get featured images response, please add _embed on the query string. example:

    http://demo.wp-api.org/wp-json/wp/v2/posts/?_embed

    Then, access the featured images in returned JSON response using _embedded['wp:featuredmedia'][0].media_details.sizes.thumbnail.source_url

    var app = angular.module('myApp', ['ngSanitize']);
        app.controller('Ctrl', function($http, $scope) {
            $http.get("http://ogmda.com/wp/wp-json/wp/v2/posts?_embed").success(function(data) {
            $scope.posts = data;
    
            var firstFeaturedImageUrl = $scope.posts[0]._embedded['wp:featuredmedia'][0].media_details.sizes.thumbnail.source_url;
        });
    });
    

提交回复
热议问题