How To... |
prev « |
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. 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 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 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 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; 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 |