Last active
December 17, 2018 14:48
-
-
Save manashathparia/265d21e5ce4eb5c95ec5bd70ccc73f44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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