Here is an example of how to use Javascript to read posts from the WordPress API.
//Pegasus = Theme name
$.getJSON('https://visionquestdevelopment.com/wp-json/wp/v2/posts', function(data){
//foreach post
for (i = 0; i < data.length; i++) {
//console.log(data[i]);
var output="";
var dateT;
var dateArr = [];
//skip one of the posts with 404s on images
if( '1555'== data[i].id ) { continue; }
//format date
var dateTime = PegasusDateTime( data[i].date );
dateArr = dateTime.toString().split(' ');
//["Day of Week", "Month", "Day", "Year", "Time", "TimeZone"]
var newTime = dateArr[1] + " " + dateArr[2] + ", " + dateArr[3];
output += "Author " + data[i].author+"<br>";
output += "Categories: " + data[i].categories+"<br>";
output += data[i].content.rendered;
output += "Date: " + newTime+"<br>";
output += data[i].excerpt.rendered;
output += "Attached Image ID: " + data[i].featured_media+"<br>";
output += "Post Format: " + data[i].format+"<br>";
//output += "GUID: " + data[i].guid+"<br>";
output += "Post ID: " + data[i].id+"<br>";
output += "Post URL: " + data[i].link+"<br>";
output += "Custom Meta: " + data[i].meta+"<br>";
output += "Slug: " + data[i].slug+"<br>";
output += "Status: " + data[i].status+"<br>";
output += "Tag IDs: " + data[i].tags+"<br>";
$(".posts").append('<li class="item">' + output + '</li>');
}
});
function PegasusDateTime(timeStr) {
//eg:-timeStr=18-03-2013 12:53:00
var tmstr = timeStr.toString().split(' '); //'21-01-2013 PM 3:20:24'
var dt = tmstr[0].split("https://wordpress.stackexchange.com/"); //
var str = dt[2] + "https://wordpress.stackexchange.com/" + dt[1] + "https://wordpress.stackexchange.com/" + dt[0] + " " + tmstr[1]; //+ " " + tmstr[1]//'2013/01/20 3:20:24 pm'
//try to create the timestamp and set time variable to valid time
var time = new Date(str);
if (time == "Invalid Date") {
time = new Date(timeStr);
}
return time;
}
https://codepen.io/jimboobrien/pen/ZKrLzV
Just modify the JS to include only the information you want.
- Jimbo