How To...
display your latest Blogger post on another page

prev «
next »
Page 1 of 1


How to display your latest Blogger post on another page

This guide requires you to have a Blogger account with your blog being published to a web server that supports PHP and that you have access to in order to upload some files. Things can be changed in this guide so it may be able to suit your situation, (i.e. you don't use Blogger, you don't use Atom, you are trying to get a post from someone else's blog and their RSS feed etc.) with a little tinkering.

If you are using Blogger as a quick and easy way to update your website, I'm sure you will appreciate the vast array of options you are given on your dashboard.
One problem I had however was that my Blogger pages are separate from the rest of my website, but I still wanted to put my most recent post on my homepage, as well as keeping my Blogger pages as they were.
While trying to get around this I came across MagpieRSS, a free to use open source project for converting various types of RSS feed. In version 0.61 Atom support was added which is the format my Blogger feed was output in so this allowed me to get the job done easily.
This is just one example of what MagpieRSS can do and even this small exercise can be highly customised to whatever style you see fit. But this is how I used it...

First of all, I assume you have downloaded the MagpieRSS files. After you have extracted them, create a directory on your web hosting server such as "magpie" and upload the following files there:

rss_cache.inc
rss_fetch.inc
rss_parse.inc
rss_utils.inc
extlib/Snoopy.class.inc

Remember that "extlib" is a subdirectory that needs to be there with "Snoopy.class.inc" in it.

Now open the web page you want to edit to include the Blogger post in your favourite editor. This file will need to be saved as a .php file if it isn't already (as opposed to .htm or .html)

Wherever you want the text to appear, put this code:

<?php
define('MAGPIE_DIR', 'yourfolder/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss( 'http://www.yoursite.com/yourfolder/atom.xml' );

//display latest blog content:
$item = $rss->items[0];
$content = $item['atom_content'];
echo "<p>Latest Blog Entry:<br>$content</p>\n";
?>

You will need to edit the parts in Red to match the directory structure on your web host. You can also edit the echo line to fit your needs, (add a style in there, or just remove the phrase "Latest Blog Entry" for instance.) Also notice that by changing the 0 in the line:

$item = $rss->items[0];

you will display an entry other than the most recent. For example, to show the third most recent post, put:

$item = $rss->items[2];

Now I'll show you how to create a list of links to the previous batch of entries, (all that are present in the Atom file.)

<?php
define('MAGPIE_DIR', 'yourfolder/magpie/');
require_once(MAGPIE_DIR.'rss_fetch.inc');

$rss = fetch_rss( 'http://www.yoursite.com/yourfolder/atom.xml' );

//display links recent blog entries:
echo "<ul><em>Latest blog additions:</em>\n";

foreach ($rss->items as $item) {
   $href = $item['link'];
   $title = $item['title'];
   $created = $item['created'];

   echo "<li><a href=$href>$title</a> $created</li>\n";
}

echo "</ul>";
?>

You should notice in the above that we are actually getting a little more information about our posts here, and then displaying it to the screen. We are using a loop to go through an array of posts one by one, getting the link to that post, the title and the date of publishing. That gets combined into an echo command to print to onto the webpage. This gets repeated to make a list, with an opening and closing before and after the loop.


Finally, what if you wanted to display a certain number of items from the feed, rather than all of them? Change this one line:

$rss = fetch_rss( 'http://www.yoursite.com/yourfolder/atom.xml' );

to add two extra lines as below:

$num_items = 5;
$rss = fetch_rss( 'http://www.yoursite.com/yourfolder/atom.xml' );
$items = array_slice($rss->items, 0, $num_items);

The 5 above says you want to get 5 posts from the feed. You also need to make one last change from this line:

foreach ($rss->items as $item) {

to be like this, which is now going through the new, smaller array you sliced from the full array:

foreach ($items as $item) {

And that is it! Upload your changes and check out your new page. Don't forget it should be a .php file now.

MagpieRSS version 0.7 has just been released this week at the time of writing and you can get it from their SourceForge group page, here. Check the "readme" file and templates for other uses and for ways to display things.

I hope this guide helped someone out there! Support open source software and services like Blogger!

Thanks
Ash - HiAsh.com