fa_1976it
fa_1976it

Reputation: 13

Wordpress json - How to use the content rendered from json

Using basic Wordpress basic Json in the format //domain.com/wp-json/wp/v2/pages/ I can access to a specific page content.

enter image description here

What I need to do is to use the rendered content as html of a blank page.

How to store this content into a variable so that I can use it? I suppose I shloud store into an array. Any sample?

Thank you.

Upvotes: 0

Views: 1383

Answers (1)

Jorge Pérez
Jorge Pérez

Reputation: 244

How to store this content into a variable so that I can use it?

var x = data.content.rendered;

Where data is the JSON object you provided. After this line is executed, x will contain HTML that you can use it in your project.

What I need to do is to use the rendered content as html of a blank page. Any sample?

//change this values
var wpApiBaseURL = "http://localhost/wp-json/wp/v2/";
var pageId = 2; // id of the page to fetch
//

var container = document.getElementById("container");

// fetch specific page
fetch(wpApiBaseURL + "pages/" + pageId)
  .then(function(rawResponse) {
    return rawResponse.json();
  })
  .then(function(jsonResponse) {
    // load successful and replaces html contents of the container div
    container.innerHTML = jsonResponse.content.rendered;
  })
  .catch(function(error) {
    container.innerText = "Error loading page";
  });;
<!DOCTYPE html>
<body>
  <div id="container">
    Loading
  </div>
</body>

Upvotes: 1

Related Questions