Displaying the list of recent posts of any CMS
The easiest way to view a list of the most recent articles, that is updated automatically, is to use the RSS feed of the site.
The script we wrote, which is based on the functions of the rsslib code of the PHP RSS reader, makes use of the RSS 2.0 feed format, which has the advantages of being the simplest and most common.
If the site is not built with a CMS that automatically generates the feed, you can use the ARA editor that from version 1.7 orders articles from newest to oldest.
This simplifies the selection of recent articles: simply load the feed
into an array and limit the number of titles you want extract, with the
function RSS_RetrieveLinks described below.
Description of the script
The script is expected to retrieve the title, link, description and date of the articles. The choice is left to the webmaster to display the information. The RSS_Recents function will be modified as needed. By default it displays only the titles.
PHP 5 is required.
RSS_Recents
That function is called for the display, and we will see the interface further.
Its role is to call RSS_RetrieveLinks to get the content from the feed and store it into an array and display it, either in the form of a succession of links, or in the form of HTML list.
RSS_RetrieveLinks
Its parameters are the URL of the feed and the number of links to return.
It loads the file with the load () method of DOMDocument (PHP 5 required), and calls RSS_Tags to retrieve the data.
RSS_Tags
It extracts information from an item in the feed, that is performed by the getElementsByTagName DOM method, and that for the title, link, etc..
Using the script
On the page that displays the list of recent articles, insert the following PHP code:
<?php require_once("recents.php"); echo RSS_Recents("https://www.scriptol.com/rss.xml", 7, false); ?>
Replace the URL to that of the feed of your site. The second parameter is the number of titles to show, the third option to place them in <li> tags or not.
In all cases, the presentation of the list will depend on a style sheet. You may insert <span> tags in the content of the $page variable to assign rules of styles to particular elements.
Demonstration of the script of recent posts
The script is based on an RSS 2.0 feed, the simplest and most common.
The feed used in this demo if that of Scriptol.com, the titles of the 7 last articles of the site are displayed.
- Download the script.
Replace then the extension .txt by .php
Optimized script of recent posts
The optimized script requires only one function.
The feed used in this demo if that of Scriptol.com, the titles of the 7 last articles of the site are displayed.