1

我正在尝试使用 Google Feed API 在我的非 wordpress HTML 网站上显示来自 wordpress 博客的最新博客文章。我设法显示了提要,但现在我也无法显示特色图像。目前它只显示最近 10 个“头条新闻”的列表,如果你愿意的话。这是代码:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript" src="http://www.google.com/jsapi"> </script>
        <script type="text/javascript">
            google.load("feeds", "1")
        </script>
<script type="text/javascript">
                        var feedUrl = "http://www.harbordev.com/blog/feed/";
                        var feedContainer=document.getElementById("feedContainer");

                        $(document).ready(function() {
                            var feed = new google.feeds.Feed(feedUrl);
                            feed.setNumEntries(10);
                            feed.load( function(result) {
                                list = "<ul>";
                                if (!result.error){
                                    var posts=result.feed.entries;
                                    for (var i = 0; i < posts.length; i++) { 
                                        list+="<li><a href='" + posts[i].link + "'target=_blank'" + "'>" + posts[i].title + "</a></li>";
                                    }
                                    list+="</ul>";
                                    feedContainer.innerHTML = list;
                                }
                                else {
                                    feedContainer.innerHTML = "Unable to fetch feed from " + feedUrl;
                                }                   
                            });
                        });
                    </script>            
</head>

<body> 
<section class="section swatch-white">
                <div class="container">
                <div class="row">
                <div class="col-md-12" id="feedContainer">

                    </div>
                </div>
                </div>
</section>
</body

我如何也显示图像?在此处的另一个条目中,我看到了这样的建议:

var bmfx = entry.mediaGroups[0].contents[0].thumbnails[0].url;

这应该工作,我会在哪里插入它?不幸的是,我的 Javascript 技能平庸,因此,如果有人能指出我正确的方向或帮助我编写一些代码,我将不胜感激。

4

1 回答 1

0

You can see the documentation of the resulting JSON here. The images are in the content field (posts[i].content):

for (var i = 0; i < posts.length; i++) { 
    list+="<li><a href='" + posts[i].link + "'target=_blank'" + "'>" + 
    posts[i].title + "</a><p>" + posts[i].content + "</p></li>";
}

If you want to show only the images, you could do that with jQuery

var images = "";
$(posts[i].content).find('img').each(function() {  
   images += this.outerHTML;
}); 

And then instead of posts[i].content, append images variable to the list

于 2014-10-05T07:33:12.340 回答