Articles

Getting Latest Posts from Blogger

COMMENTS
(0) Comments
I've done this in a few different ways, but I love the simplicity of the following example. I've included the whole page of code to show you what goes where. Be sure to change the blogID to match your own blog.

<!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 SampleLast 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(feedUriprocessFeedhandleError);
  
}

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

  
function processFeed(feedRoot{
    
var entries feedRoot.feed.getEntries();
    for (var 
0entries.length && entriesToRetrievei++) {
      
// Locate URI to blog post
      
var links entries[i].getLinks();
      var 
postLink null;
      for (var 
0links.length && !postLinkj++) {
        
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

Comments

Name:

Email:

URL:

Comments:

Remember my personal information

Notify me of follow-up comments?

Please enter the word you see in the image below: