Camen Design Forum

Delete

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

To delete this post you must be either the original author or a designated moderator.
The content of the post will be removed but the name and date will remain.

  • The post will be removed completely from the thread, rather than blanked
  • Only posts on the last page of the thread can be removed completely (so as to not break permalinks)

RE[6]: 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.

Your friendly neighbourhood moderators: Kroc, Impressed, Martijn