Search code examples
javascriptwordpressfetchwordpress-rest-api

Fetch WordPress API call :: how to get it to actually display on the page


https://codepen.io/Mortiferr/pen/XYNLpK?editors=1011

I'm trying to get the posts to display here and I cannot seem to make it work. If I console.log data[0].title it works and gives the data I ask for. But when I try and display it on the page...no dice. See codepen for details.


Solution

  • It looks like data.title is an object with rendered inside.

    data[0].title.rendered will do the work

    $(document).ready(function(){
      fetch('https://www.perfectimprints.com/blog/wp-json/wp/v2/posts/')
      .then(response => response.json())
      .then(data => {
        // Here's a list of repos!
        $("#postTitle").append(data[0].title.rendered);
      });
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="postDate"></div>
    <div id="postTitle"></div>