Show Latest Blog Posts on any PHP page
You can use magpie to display your latest blog posts on any php page, so a site without a blog can still display posts from any blog that has an RSS feed. Magpie tutorials on the net usually don’t take into consideration quotes which we have here, so this should be all you need to get going.
Magpie & RSS
If you’ve come to this page you probably already use magpie, if not, magpie is an RSS parser that can be used to display your blog posts on any php page. You can get Magpie here
Complete Code
You don’t need to know what any of this stuff means really, just change path/that/points/to/magpie/rss_fetch.inc to be the actual path to that file on your server, set the $url variable to point to your RSS feed, and $num_items sets the number of posts you want to see.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<dl> <?php define('MAGPIE_INPUT_ENCODING', 'UTF-8'); define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); require_once 'path/that/points/to/magpie/rss_fetch.inc'; $url = 'http://feeds.mimoymima.com/mym-news?format=xml'; $num_items = 5; $rss = fetch_rss($url); $rss->items = array_slice($rss->items, 0, $num_items); foreach ($rss->items as $item ) { $title = $item[title]; $link = $item[link]; $description = $item[description]; $pubdate = $item[pubdate]; $title = htmlentities($title, ENT_QUOTES, 'UTF-8'); $link = htmlentities($link, ENT_QUOTES, 'UTF-8'); preg_match('/[0-9]+/', $pubdate, $pubDay); preg_match('/[A-Za-z]+s/', $pubdate, $pubMonth); preg_match('/[0-9]{4}/', $pubdate, $pubYear); $shortDate = "$pubMonth[0]" . "$pubDay[0], " . "$pubYear[0]"; echo "<dt><a href="$link" class="Popup">$title</a></dt>"; echo "<dd class="PubDate">$shortDate</dd>"; echo "<dd class="Description">$description</dd>"; } ?> </dl> |
The Curly Quote Fixes
If you already use magpie to display your blog posts and just need to fix your quotes, then you would just add these pieces from the code above to your code:
1 2 3 4 |
define('MAGPIE_INPUT_ENCODING', 'UTF-8'); define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
and
1 2 3 4 |
$title = htmlentities($title, ENT_QUOTES, 'UTF-8'); $link = htmlentities($link, ENT_QUOTES, 'UTF-8'); |
If you run PHP5 on your server, it comes with it’s own simple to use parser called SimpleXML which you could use instead of magpie, we’ll show you how to do that in a future article, so subscribe to our feed if you want to be notified.
Topics covered: BlogMagpiephprssXML