Monday, March 5, 2012

Blogger: retrieve the list of all posts


To retrieve the posts of a blog, we can use the URL like this:

http://***.blogspot.com/feeds/posts/default?alt=json&callback=mycallbackfunc&start-index=1&max-results=100

However, it does not work if your blog has more than 500 posts and you want to list them all. My experiment showed that even you supplied a big number (e.g. 10000) for max-results, only up to 500 posts would return. To retrieve the list of more than 500 posts, we need a loop to do that. The following is how I implemented it for my blog. You can see the result by clicking the *All Posts* page link at the top left corner of this page.

<div><ul id="postList12"></ul></div>

<script type="text/javascript">

var startIndex = 1;
var maxResults = 100;

function sendQuery12()
{
   var scpt = document.createElement("script");
   scpt.src = "/feeds/posts/summary?alt=json&callback=processPostList12&start-index=" + startIndex + "&max-results=" + maxResults;

   document.body.appendChild(scpt);
}

function processPostList12(root)
{
   var elmt = document.getElementById("postList12");
   if (!elmt)
      return;

   var feed = root.feed;

   if (feed.entry.length > 0)
   {
      for (var i = 0; i < feed.entry.length; i++)
      {
         var entry = feed.entry[i];

         var title = entry.title.$t;

         for (var j = 0; j < entry.link.length; j++)
         {
            if (entry.link[j].rel == "alternate")
            {
               var url = entry.link[j].href;

               if (url && url.length > 0 && title && title.length > 0)
               {
                  var liE = document.createElement("li");

                  var a1E = document.createElement("a");
                  a1E.href = url;
                  a1E.textContent = title;

                  liE.appendChild(a1E);

                  elmt.appendChild(liE);
               }

               break;
            }
         }
      }

      if (feed.entry.length >= maxResults)
      {
         startIndex += maxResults;
         sendQuery12();
      }
   }
}

sendQuery12();

</script>

17 comments:

Orilea said...

Hi there. I tried to implement this, but it's not working in Windows/IE9. (Even though it works fine -and looks great- in my Safari, Firefox and Chrome.) Any idea what's wrong? Thanks for your help!

www.orileaoutloud.com

Zen said...

Hi Orilea, if you use your IE9 to open my All Posts link on the top left of the page, do you see the result of all my posts list? If not, it could be caused by the configuration of your IE. Do you disable its Javascript?

Soulreaper said...

how to arrange the posts list in alphabetical order ?

Iurie Malai said...

Hi! I implemented your code for my 404 Custom Page (Not Found) (try this) and I have there all my posts (2 actually) listed twice. How to avoid this?

Dejaime Neto said...

Hi, I thank you for the script, works like a charm. It's copy, paste, watch.

Anonymous said...

Same. Thank you very much! My old (copied) code for doing this just expired.

This code still works (Firefox 01-06-14).

Anonymous said...

THAAAAAAAAAAAAANNNNNKKKKKKKKKXXXXXXXXXXXXXXX

ALOT SEARCHING FOR THIS FROM A LONG TIME!IT WORKED!!!!

Tsick said...

Thank you !

I have a question, is it possible to arrange the list in alphabetical order ?

May be with a sort function ? But where insert that function in the code ?

Stouffville Electrical Contractor North said...


Zen, pls add to your title "create list of posts titles for blogspot blog" and "how to make a list of posts titles for blogger".

Because it is very hard to find your invaluable post!

and pls add this to your code:

to hide bullets

add elmt.style.listStyleType = 'none';

just below var elmt = document.getElementById("postList12");

Michio said...

for a sorted list paste this into the script tag
http://stackoverflow.com/a/18215065

and insert
sortUnorderedList(elmt, false);
above the last curly bracket.

Jon Boeckenstedt said...

Thank you for doing this. I've tried many different scripts but they never seem to work. This did it for me in a second!

briscoe04 said...

I am a bit inexperienced with Blogger. Where do I put the code, and how do I get the "All Posts" link to show up in my menu?

Zen said...

You can create a new Blogger post. Click on the HTML button on the toolbar. And then paste the code inside.

Internet Knjižara 365 said...

I want alphabetic sort, but I do not now how to make it according to the comment of Michiro.

"for a sorted list paste this into the script tag
http://stackoverflow.com/a/18215065
and insert
sortUnorderedList(elmt, false);
above the last curly bracket."

Does anyone know to do this?

zeeshan adil said...

works wonder. very easy pzy.

Silviu Andrei said...

Works like a charm in a static page.
I wonder if it's possible to get the list by alphabetical order ?
Or maybe, to open links in a new window?
Thx

Phil Trav said...

This scrit does not work for me in Blogger.com on the website of my friends at http://www.philippinestravel.info

I tried this code in both a post and a page but it did not work, I also tried just running the url as there is less than 100 posts to date but that also did not work. I wonder if it does not work with some templates or if this is outdated now?

 
Get This <