Camen Design Forum

How to get latest posts

Jos

Yes. This only scans subfora and only one level down. It you have nested fora, probably a good idea with many fora, then you probably need to use http://php.net/manual/en/class.recursivedirectoryiterator.php.
For me the above is very simple and works wonderful in my setup.

I've cleaned the code a bit this morning and put it in a function.

%
<?
function latestFeed($n) {
	$latest[]=NULL;
	foreach (glob("forum/*/index.xml") as $filename) {
		$feeds[] = $filename;
		}
// Get all feed entries
	$entries = array();
	foreach ($feeds as $feed) {
    	$xml = simplexml_load_file($feed);
    	$entries = array_merge($entries, $xml->xpath('/rss//item'));
	}

// Sort feed entries by pubDate (descending)
	usort($entries, function ($x, $y) {
    	return strtotime($y->pubDate) - strtotime($x->pubDate);
	});

	foreach (array_slice($entries,0,$n) as $entry){
		$entry->description=strip_tags(mb_substr($entry->description, 0, 100, 'UTF-8')) . '...';
		$latest[]=$entry;
	}
return $latest;
}
?>
%

I call the above with the following:

%
    require_once 'forum/joinfeeds.php';
    $latestfeeds=latestFeed(5);
%

to get the latest 5 posts.

Append

(Leave this as-is, it’s a trap!)

Only the original author or a moderator can append to this post.

Pro tip: Use markup to add links, quotes and more.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn