xml - Parsing WordPress RSS Feed with JQuery -
i using jquery read , parse self-hosted wordpress atom feed. here's example of xml generated wordpress:
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xml:lang="en-us" xml:base="http://example.com/wp/wp-atom.php" > <title type="text">feed title</title> <subtitle type="text">feed subtitle</subtitle> <updated>2013-04-19t21:33:48z</updated> <link rel="alternate" type="text/html" href="http://example.com/wp" /> <id>http://example.com/wp/index.php/feed/atom/</id> <link rel="self" type="application/atom+xml" href="http://example.com/wp/index.php/feed?feed=atom&cat=10" /> <generator uri="http://wordpress.org/" version="3.5.1">wordpress</generator> <entry> <author> <name>author name</name> </author> <title type="html">article title</title> <link rel="alternate" type="text/html" href="http://example.com/wp/index.php/2013/04/19/article-title/" /> <id>http://example.com/wp/?p=1418</id> <updated>2013-04-19t21:33:48z</updated> <published>2013-04-19t21:33:16z</published> <category scheme="http://example.com/wp" term="firstcategory" /> <category scheme="http://example.com/wp" term="secondcategory" /> <category scheme="http://example.com/wp" term="thirdcategory" /> <summary type="html">a summary excerpt of article [...]</summary> <content type="html" xml:base="http://example.com/wp/index.php/2013/04/19/tons-vs-tonnes/">a summary excerpt of article whole bunch more text taht not in summary. </content> <link rel="replies" type="text/html" href="http://example.com/wp/index.php/2013/04/19/article-title/#comments" thr:count="0"/> <link rel="replies" type="application/atom+xml" href="http://example.com/wp/index.php/2013/04/19/article-title/feed/atom/" thr:count="0"/> <thr:total>0</thr:total> </entry> </feed>
here's jquery ajax code consume feed , display it:
$.ajax({ url: "//example.com/wp/index.php/feed?feed=atom&cat=10", datatype: "xml", success: function(data) { console.log(data); var $xml = $(data), items = []; $xml.find("entry").each(function(i) { if (i === 30) return false; var $this = $(this); console.log($this); items.push('<div><h3>' + $this.find("title").text() + '</h3><p><small>published ' + $this.find("author").text().trim() + ' on ' + publisheddate.todatestring() + ' ' + publisheddate.totimestring() + '</small></h3>' + $this.find("content").text() + '</div><hr />'); }); $("#displaydiv").html(items.join(''));
this working!! want enhance list of category tags entry follows:
firstcategory | secondcategory | thirdcategory
if @ xml, notice there 3 category elements in sample entry.
i've tried several methods , have failed miserably. i've reverted working code have extracted above sample javascript, don't have failed attempts left show you.
in code use publisheddate.totimestring()
gives error.
to catgeories use like:
var categories = ""; $this.find("category").each(function(j) { categories += " " + ($(this).attr("term")); }) items.push("categories:" + categories);
Comments
Post a Comment