使用WP API我試圖從後得到的特色形象,但不成功 - 這裏的代碼不工作行:WP API功能的圖像附件
ourHTMLString += postsData[i]._links[i].wp:featuredmedia[i].href.guid.rendered;
其他行的代碼正在工作。下面是代碼:
var prodCatPostsContainer = document.getElementById("prod-Cat-Posts-Container");
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'www.example.com/wp-json/wp/v2/posts?filter[category_name]=news-and-events');
function createHTML(postsData) {
var ourHTMLString = '';
for (i = 0;i < postsData.length;i++) {
ourHTMLString += postsData[i]._links[i].wp:featuredmedia[i].href.guid.rendered;
ourHTMLString += '<h6 class="news-title"><a href="#">' + postsData[i].title.rendered + '</a></h6>' ;
ourHTMLString += postsData[i].content.rendered;
}
prodCatPostsContainer.innerHTML = ourHTMLString;
}
ourRequest.onload = function() {
if (ourRequest.status >= 200 && ourRequest.status < 400) {
var data = JSON.parse(ourRequest.responseText);
console.log(data);
createHTML(data);
} else {
console.log("We connected to the server, but it returned an error.");
}
};
ourRequest.onerror = function() {
console.log("Connection error");
};
ourRequest.send();
UPDATE
我又增加XMLHttpRequest來獲取媒體功能的新聞項目的形象,每@RYAN AW建議,但還是行不通。我不確定我是否正確執行此操作,但是我將所有特色媒體ID都推送到了一個數組中,然後使用數組中的ID作出獲取請求,獲取「guid」 - >「rendered」圖像url我可以在JSON中看到。我是否必須以某種方式循環瀏覽相關新聞項目mediaRequest?即mediaRequest.open('GET', 'http://www.example.com/wp-json/wp/v2/media/' + featuredMedia[i]);
任何幫助都會很棒。
var prodCatPostsContainer = document.getElementById("prod-Cat-Posts-Container");
var mediaContainer = document.getElementById("media-Container");
var featuredMedia = [];
//----------------- News Content ------------------//
var newsRequest = new XMLHttpRequest();
newsRequest.open('GET', 'http://www.example.com/wp-json/wp/v2/posts?filter[category_name]=news-and-events');
newsRequest.onload = function() {
if (newsRequest.status >= 200 && newsRequest.status < 400) {
var data = JSON.parse(newsRequest.responseText);
createNEWS(data);
} else {
console.log("News Request - We connected to the server, but it returned an error.");
}
};
function createNEWS(postsData){
var ourHTMLString = '';
for (i = 0;i < postsData.length;i++){
featuredMedia.push(postsData[i].featured_media);
ourHTMLString += '<h6 class='"news-title"'><a href="#">' + postsData[i].title.rendered + '</a></h6>' ;
ourHTMLString += postsData[i].content.rendered + '<br><br>';
}
prodCatPostsContainer.innerHTML = ourHTMLString;
}
newsRequest.onerror = function() {
console.log("Connection error");
};
newsRequest.send();
//----------------- Media Featured Image ------------------//
var mediaRequest = new XMLHttpRequest();
mediaRequest.open('GET', 'http://www.example.com/wp-json/wp/v2/media/' + featuredMedia);
/*for (i = 0;i < featuredMedia.length;i++){
mediaRequest.open('GET', 'http://www.example.com/wp-json/wp/v2/media/' + featuredMedia[i]);
}*/
mediaRequest.onload = function() {
if (mediaRequest.status >= 200 && mediaRequest.status < 400) {
var mediaDat = JSON.parse(mediaRequest.responseText);
createMEDIA(mediaDat);
} else {
console.log("Media Request - We connected to the server, but it returned an error.");
}
};
function createMEDIA(mediaData){
var mediaHTMLString = '';
for (i = 0;i < mediaData.length;i++){
mediaHTMLString += '<img src="' + mediaData[i].guid.rendered + '"/><br>';
}
mediaContainer.innerHTML = mediaHTMLString;
}
mediaRequest.onerror = function() {
console.log("Connection error");
};
mediaRequest.send();
什麼是錯誤代碼。將此代碼添加到我的函數的文件嗎?如果存在 –
'GET ... net :: ERR_CONNECTION_REFUSED' - 我已成功獲取featured_media id - 'ourHTMLString + = postsData [i] .featured_media ;''你是說你不能得到IMG網址?不太清楚你的意思 – roshambo
是的,如果你需要媒體網址你需要做第二次請求或修改響應函數或使用插件更好的休息api精選圖像 –