2011-04-27 28 views
1

我在WordPress博客中成功啓用了json API,但我很難用jQuery返回數據。WordPress jSON API如何使用jQuery返回數據?

http://example.com/?json=1&count=1&include=title

這裏是JSON格式。理想情況下,我想追加一個名爲#homeblog的div,最後一個帖子標題鏈接到帖子本身。

{ 
    "status": "ok", 
    "count": 1, 
    "count_total": 1, 
    "pages": 1, 
    "posts": [ 
    { 
     "id": 1, 
     "type": "post", 
     "slug": "hello-world", 
     "url": "http:\/\/localhost\/wordpress\/?p=1", 
     "title": "Hello world!", 
     "title_plain": "Hello world!", 
     "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n", 
     "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n", 
     "date": "2009-11-11 12:50:19", 
     "modified": "2009-11-11 12:50:19", 
     "categories": [], 
     "tags": [], 
     "author": { 
     "id": 1, 
     "slug": "admin", 
     "name": "admin", 
     "first_name": "", 
     "last_name": "", 
     "nickname": "", 
     "url": "", 
     "description": "" 
     }, 
     "comments": [ 
     { 
      "id": 1, 
      "name": "Mr WordPress", 
      "url": "http:\/\/wordpress.org\/", 
      "date": "2009-11-11 12:50:19", 
      "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.<\/p>\n", 
      "parent": 0 
     } 
     ], 
     "comment_count": 1, 
     "comment_status": "open" 
    } 
    ] 
} 

回答

2

如果要追加由JSON API返回到你需要的東西就像一個頁面的一些數據:

$(document).ready(function(){ 
    $.getJSON("http://example.com/?json=1&count=1",function(data) { 
     $('body').append('<a href="'+data.posts[0].url+'"><div id="homeblog">'+data.posts[0].title+'</div></a>'); 
    } 

我已刪除了JSON調用的include=title部分原因是排除了一切。您正在使用getJSON返回後data,那麼它只是一個它的地方荷蘭國際集團到頁面(在我的情況右轉上<body>標籤)

編輯構建你的HTML的情況下和append:有是WordPress的一個wordpress stackexchange的問題。你會有更多的運氣在這邊問這樣的問題。 (加右括號)