Created
April 11, 2013 21:16
-
-
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 …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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