Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fitzage
Created April 11, 2013 21:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fitzage/5367242 to your computer and use it in GitHub Desktop.
Save fitzage/5367242 to your computer and use it in GitHub Desktop.
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