我正在使用YQL在我的wordpress网站上解析RSS提要。我有代码工作并显示我的RSS提要中的条目,但我如何限制它显示的条目数?有没有办法一次显示5个?我的代码如下:
function parseFeed(url, container) {
// yql query
var query = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from feednormalizer where url="' + url + '"' ) + '&format=json';
// send request
$.getJSON(query, function (data, status, errorThrown) {
// if successful... *
if (status === 'success') {
// log object data in console
console.log(data);
// append feed link and title in container
$(container).append('<a href="' + url + '"><span class="iconicstroke-rss-alt"></span></a>');
$(container).append('<h1 class="feed">' + data.query.results.rss.channel.title );
// for each entry... *
$.each(data.query.results.rss.channel.item, function (key, value) {
// * create new date object and pass in entry date
var date = new Date(value.pubDate);
// * create months array
var months = new Array(12);
months[0] = 'January';
months[1] = 'February';
months[2] = 'March';
months[3] = 'April';
months[4] = 'May';
months[5] = 'June';
months[6] = 'July';
months[7] = 'August';
months[8] = 'September';
months[9] = 'October';
months[10] = 'November';
months[11] = 'December';
// * parse month, day and year
var month = date.getMonth();
var day = date.getDate();
var year = date.getFullYear();
// * build content snippet
var content = $(value.description).text().substring(0, 340);
if (value.description.length > content.length ) {
content += ' ...';
}
// * assign entry variables
var title = '<h3 class="title"><a href="' + value.link + '" target="_blank">' + value.title + '</a></h3>';
var time = '<p class="time">' + day + ' ' + months[month] + ' ' + year + '</p>';
var snippet = '<p class="snippet">' + content + '</p>';
var entry = '<div class="entry">' + title + time + snippet + '</div>';
// * append entire entry in container
$(container).append(entry);
});
// if there's an error... *
} else if (status === 'error' || status === 'parsererror') {
// * log error message in console
console.log(errorThrown);
// * show error message
alert('Could not load RSS feed!');
}
});
}
$(document).ready(function () {
parseFeed('http://www.vatlive.com/feed/', '#vatrss', '');
});