2013-10-08 57 views
0

我目前正在開發一個應用程序來檢索來自WordPress的網站的訂閱源,並以jquery移動列表格式列出各個帖子。下面是JS代碼:所有動態帖子默認爲第一個對象

$(document).ready(function() { 
    var url = 'http://howtodeployit.com/category/daily-devotion/feed/'; 
    $.ajax({ 
     type: "GET", 
     url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&output=json_xml&num=1000&callback=?&q=' + encodeURIComponent(url), 
     dataType: 'json', 
     error: function() { 
      alert('Unable to load feed, Incorrect path or invalid feed'); 
     }, 
     success: function (data) { 
      var postlist = data.responseData.feed.entries; 
      var html = '<ul data-role="listview" data-filter="true">'; 
      for (var i = 0; i < 6; i++) { 
       var entry = postlist[i]; 
       console.log(entry); 
       html += '<li>'; 
       html += '<a href="#articlepost" onclick="showPost(' + entry.id + ')">'; 
       html += '<div class="etitle">' + entry.title + '</div>'; 
       html += '<div class="esnippet">' + entry.contentSnippet + '</div>'; 
       html += '</a>'; 
       html += '</li>'; 
      } 
      html += '</ul>'; 
      $("#devotionlist").append(html); 
      $("#devotionlist ul[data-role=listview]").listview(); 
     } 
    }); 
}); 

function showPost(id) { 
    $('#articlecontent').html("Loading post..."); 
    $.getJSON('http://howtodeployit.com/category/daily-devotion/?json=get_post&post_id=' + id + '&callback=?', function (data) { 
     var html = ''; 
     html += '<h3>' + data.post.title + '</h3>'; 
     html += data.post.content; 
     $('#articlecontent').html(html); 
    }); 
} 

當我點擊任何顯示的6個員額中,只有第一篇文章的內容被顯示,而不是個別職位的內容。

+1

您確認服務器正在返回一個不同的帖子?你有沒有證實你沒有通過每個鏈接相同的ID? – Gregg

+0

將XML提要轉換爲JSON時,它看起來像Google API,但不包含帖子ID。希望使用谷歌的'google.feeds.Feed.MIXED_FORMAT'來獲得額外的XML屬性,所以我可以得到元素並從中提取帖子ID,但沒有工作 – Chelseawillrecover

+0

我終於解決了這個問題,我有已經發布在另一篇文章中。我也會在這裏發佈給其他人看 – Chelseawillrecover

回答

0

我該如何解決這個問題?

第一步:從我的WordPress的永久鏈接設置,我選擇了自定義結構和補充/%postname%/%POST_ID%這意味着我的RSS XML輸出結果我的「鏈接」元素將在以下格式:

<myurl>/<postname>/<postID> (http://howtodeployit.com/xxx/111/) 

第二步:爲了讓我更容易,而不是寫我用了一個拆分命令這樣的正則表達式查詢:

var postlink = entry.link; 
var id = postlink.split(/\//)[4]; 

(///)[4]只會通過拆分網址斜槓的數量,並採取只是第四位,這是我的postID的地方。

我希望這適用於我的位置的其他人