Include fetch
<script type="module" src="https://rawgit.com/github/fetch/master/fetch.js"></script>
Fetch posts
fetch('https://public-api.wordpress.com/rest/v1.1/sites/potlachsite.wordpress.com/posts/').then(function(response){
if (response.status >= 200 && response.status < 300) {
return response.json();
} else {
var error = new Error(response.statusText)
error.response = response
throw error
}
}).then(printList).catch(console.log);
function printList(j){
var posts = j.posts.map(function(p) {
var article = document.createElement('article');
var categories = Object.keys(p.terms.category).join(', ') || '';
var firstTags = Object.keys(p.tags)[0] || 0;
article.innerHTML = "<header><h2>" + p.title + "</h2>" +
"<p>Categoria: " + categories + "<br>Prezzo: " + firstTags + " €</p>" +
"</header>";
article.innerHTML += p.content;
if(p.post_thumbnail) article.innerHTML += "<br>thumbnail: " + p.post_thumbnail.URL;
return article;
});
var parent = document.querySelector('.markdown-body');
var reference = document.querySelector('hr');
var append = posts.map(function(e){
parent.insertBefore(e ,reference);
});
}
Post data
{
title: "...",
content: "...",
tags: {
15.30: {...}
},
terms: {
category: {
Cani: {...}
}
},
post_thumbnail: {
URL: "..."
}
}
ef6d1ee13f6ff4fb52d41d4fd9293f3161cc3c04