0

I want to use the REST API of wordpress to build a new version of a custom theme. I'm able to get the json response of a particular category, but I need to find a way to obtain also the featured image or the second post thumbnail registered using a plugin, so I can use them in the layout. Is this possible without using a plugin?

here is the code I'm using for testing:

<script>
(function($){
  $(document).ready(function(){

    $.getJSON('https://localhost/wordpress/wp-json/wp/v2/posts?category_name=portfolio&per_page=50&_embed', function(response){
      console.log(response);
      $.each(response, function(i){
        console.log(response[i]._embedded['wp:featuredmedia'][0].source_url);
      });
    });

  });
}(jQuery));
</script>

the last part of the console log will not work but the [0].source_url is the info I need for each JSON post provided.

13
  • You'll need to add that field in to the REST API response, but keep in mind that you should probably raise this as a feature request for that plugin via their support mechanism. If there was a plugin that would do this, it would be the plugin you're using, or from the same vendor. I think the best you can hope for is how to add fields to the response the REST API gives, or add a piece of post meta to it, otherwise the specifics of this specific plugin you're using require speciailist knowledge of that plugin which would be offtopic on this site
    – Tom J Nowell
    Nov 12, 2019 at 19:06
  • I'm not using a plugin, I wrote my own if needed or use the native CMS functions. I want only to use the source_url to obtain the url from the response, it will give me an undefined error for now i chrome console.
    – sialfa
    Nov 12, 2019 at 19:17
  • hmm if the source_url doesn't work, how did you come upon it to begin with? Note that the the second post thumbnail registered using a plugin, part can't be answered at the moment as there's zero information about the plugin you wrote and how it stores information
    – Tom J Nowell
    Nov 12, 2019 at 19:57
  • ´source_url´ is a parameter that will be returned from the API key when the _embed querystring param is used. For the second plugin is the famous Multiple post thumbnail, but I don't have a link to it
    – sialfa
    Nov 12, 2019 at 21:52
  • 1
    solved!thank you
    – sialfa
    Nov 14, 2019 at 5:21

1 Answer 1

0
(function($){
  $(document).ready(function(){
    var items = [];
    $.getJSON('https://localhost/wordpress/wp-json/wp/v2/posts?category_name=portfolio&per_page=50&_embed', function(response){
      console.log(response);
      $.each(response, function(key,val){
        //console.log(val._embedded['wp:featuredmedia'][0].source_url);
        items.push(''+''+
                     '');
      });
    });

  });
}(jQuery));

//use variable array to push data
1
  • 2
    It might be helpful if you explained what this code does so that @sialfa can understand what to do to it if it ever needs to be modified in the future.
    – Mort 1305
    Jun 21, 2020 at 18:48

Your Answer

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

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