3 messages in com.googlegroups.bloggerdev[bloggerDev] Re: How do I get the las...
FromSent OnAttachments
jlhall09 Jul 2008 15:14 
Trevor Johns10 Jul 2008 22:37 
Trevor Johns11 Jul 2008 01:06 
Subject:[bloggerDev] Re: How do I get the last few recent posts?
From:Trevor Johns (tjo@google.com)
Date:07/10/2008 10:37:15 PM
List:com.googlegroups.bloggerdev

On Fri, Jun 27, 2008 at 1:45 PM, jlhall <jlha@gmail.com> wrote:

I want to have a page pull the last three blog entries from my blog. I just want the date it was posted and the title that is linked to the post. What is the best way to do this? It seems simple but I can't find anything out there.

Thanks

Hello, The easiest way to do this is using the JavaScript API. Here's an example (be sure to change the blogId variable, you can find the correct value by looking at the source of your blog and searching for EditURI):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>Google Data API Sample: Last Three Blog Posts</title> <script src="http://www.google.com/jsapi" type="text/javascript" charset="utf-8"></script> <script type="text/javascript" charset="utf-8"> // Configuation - Edit these values to match your blog! blogId = "32786009"; var entriesToRetrieve = 3;

// Load Google Data library and set callback google.load("gdata", "1"); google.setOnLoadCallback(populatePosts);

function populatePosts() { // Create service object var service = new google.gdata.blogger.BloggerService("exampleCo-exampleApp-1.0");

// Retreive list of blogs var feedUri = "http://www.blogger.com/feeds/" + blogId + "/posts/default"; service.getBlogFeed(feedUri, processFeed, handleError); }

function handleError(errorMsg) { // Display an alert dialog with the error text alert(errorMsg); }

function processFeed(feedRoot) { var entries = feedRoot.feed.getEntries(); for (var i = 0; i < entries.length && i < entriesToRetrieve; i++) { // Locate URI to blog post var links = entries[i].getLinks(); var postLink = null; for (var j = 0; j < links.length && !postLink; j++) { if (links[j].getRel() == "alternate" && links[j].getType() == "text/html") postLink = links[j].getHref(); }

// Create hyperlink var linkNode = document.createElement("a"); linkNode.setAttribute("href", postLink);

// Create text var textNode = document.createElement("p"); textNode.appendChild(document.createTextNode(entries[i].getTitle().getText() + " (" + formatDate(entries[i].getPublished().getValue().getDate()) + ")")); linkNode.appendChild(textNode);

// Write to page document.getElementById('post_container').appendChild(linkNode); } }

function formatDate(date) { return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDay(); }

</script> </head>

<body>

<p>Blog posts:</p>

<div id="post_container"></div>

</body> </html>

WebKit-based browsers aren't supported yet, as well as users who have JavaScript disabled. If your server supports server-side scripts, you could use one of the other client libraries (Python, PHP, etc.) instead to get around this limitation. Let me know if you want more information on this.

Happy coding!