-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordpress-posts.js
93 lines (86 loc) · 3.65 KB
/
wordpress-posts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class BlogPost {
constructor(title, desc, date, img, address) {
this.postTitle = title;
this.description = desc;
this.postDate = date;
this.image = img;
this.url = address;
}
}
function latest_blog_posts() {
let array = [];
var title;
var description;
var link_url;
var date;
var img_link;
$.getJSON('http://mateuszmigot.pl/blog/wp-json/wp/v2/posts?per_page=3&_embed', function (data) {
$.each(data, function (index, jsonData) {
$.each(jsonData, function (key, value) {
if (key == "date") {
var index = value.indexOf('T');
value = value.substring(0, index);
date = value;
}
if (key == "title") {
$.each(value, function (key, value) {
title = value;
});
}
if (key == "link") {
link_url = value;
}
if (key == "excerpt") {
$.each(value, function (render, desc) {
if (render == "rendered") {
var pstart = desc.substring(24, desc.size - 3);
desc = desc.replace(pstart, '');
var pend = desc.substring(desc.length - 5);
desc = desc.replace(pend, '');
description = desc;
}
});
}
if (key == "_embedded") {
$.each(value, function (keys, mediaData) {
if (keys == "wp:featuredmedia") {
$.each(mediaData, function (pomIndex, keyValues) {
$.each(keyValues, function (keyValue, properValue) {
if (keyValue == "link") {
img_link = properValue;
var newPost = new BlogPost(title, description, date, img_link, link_url);
array.push(new BlogPost(title, description, date, img_link, link_url));
}
});
});
}
});
}
});
});
console.log(array);
show_blog_posts(array);
});
}
latest_blog_posts();
function show_blog_posts(array) {
var classes = ["animate__fadeInLeft", "animate__fadeInUp", "animate__fadeInRight"];
var keys = Object.keys(array);
var docFrag = document.createDocumentFragment();
for (var i = 0; i < keys.length; i++) {
var tempNode = document.querySelector("div[data-type='template']").cloneNode(true);
tempNode.querySelector(".card").className += classes[i];
tempNode.querySelector("h5.card-title > a").textContent = array[i].postTitle;
tempNode.querySelector("h5.card-title > a").href = array[i].url;
tempNode.querySelector("h5.card-title > a").title = array[i].postTitle;
tempNode.querySelector("p.card-text").textContent = array[i].description;
tempNode.querySelector("small.text-muted").textContent = array[i].postDate;
tempNode.querySelector("img").src = array[i].image;
tempNode.querySelector("button > a").href = array[i].url;
tempNode.querySelector("a.img-link").href = array[i].url;
tempNode.style.display = "block";
docFrag.appendChild(tempNode);
}
document.getElementById("blog-posts-section").appendChild(docFrag);
delete docFrag;
}