Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Kirby plugin to pull a specified number of posts either from a specific category, tag, or just in general that are the latest ones excluding any posts that are in the future. The date format is set for my blog post date format, but you can modify it to fit your needs. Use either a 'category' or 'tag' option to specify a category or tag, and use …
<?
function latestnotfuture($articles, $options=array()) {
global $site;
$defaults = array(
'category' => '',
'num' => '1',
'dateformat' => 'Y-m-d H:i',
'tag' => ''
);
$options = array_merge($defaults, $options);
if ($options['category'] != '') :
$masterlist = $articles->sortBy($sort='date', $dir='desc')->filterBy('categories',$options['category'],',');
elseif ($options['tag'] != ''):
$masterlist = $articles->sortBy($sort='date', $dir='desc')->filterBy('tags',$options['tag'],',');
else :
$masterlist = $articles->sortBy($sort='date', $dir='desc');
endif;
$now = strtotime(date($options['dateformat']));
$currentarticles = array();
foreach($masterlist AS $article):
$date = strtotime($article->date($options['dateformat']));
if($date <= $now):
$currentarticles[] = $article;
endif;
endforeach;
if ($options['num'] == '1') :
return $currentarticles[0];
else:
return array_slice($currentarticles, 0, $options['num']);
endif;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment