Skip to content

Instantly share code, notes, and snippets.

@manashathparia
Last active December 17, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manashathparia/265d21e5ce4eb5c95ec5bd70ccc73f44 to your computer and use it in GitHub Desktop.
Save manashathparia/265d21e5ce4eb5c95ec5bd70ccc73f44 to your computer and use it in GitHub Desktop.
class RecentPosts {
constructor(element, number, url) {
const div = document.querySelector(element);
this.url = url || window.location.origin;
this.getPosts()
.then(data => this.removeUnwanted(data, number))
.then(data => {
data.forEach(post => {
if (post)
div.innerHTML =
div.innerHTML + `<li><a href=${post.link}>${post.title}</a></li>`;
});
});
}
async getPosts() {
const posts = await fetch(this.url + "/wp-json/wp/v2/posts");
return posts.json();
}
removeUnwanted(data, number) {
return data.map(({ title: { rendered: title }, link }, i) => {
if (i < number) {
return {
title,
link
};
}
return;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment