Last active
December 14, 2015 09:49
example to render today posts with prev day next day links if found . with url segments for category and day date
This file contains 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
<?php | |
/* | |
* as per http://processwire.com/talk/topic/2928-pagination-by-date/ | |
* list posts by day, show prev next day if any posts found | |
* | |
* - posts have a date field "publish_from" | |
* with no time selection but only days | |
* this will save the timestamp of today 00:00 (starting) so we can simply | |
* use "publish_from=$prevday" in selectors | |
* | |
* - posts have page fields "categories" | |
* | |
* urls like: | |
* /posts/[category]/[dd-mm-yyyy]/ | |
* | |
* enable url segments on posts template | |
* | |
*/ | |
// check urls segments | |
if($input->urlSegment1){ | |
$cat = $sanitizer->pageName($input->urlSegment1); | |
} else { | |
$cat = "training"; // default | |
} | |
if($input->urlSegment2) { | |
$day = $sanitizer->pageName($input->urlSegment2); | |
} else { | |
$day = date("d-m-Y", strtotime("today")); // default | |
} | |
// get the category page, if not found any show 404 | |
$category_page = $pages->get("template=category, name=$cat"); | |
if(!$category_page->id) { | |
// throws 404 when category not found | |
throw new Wire404Exception("Page Not Found"); | |
} | |
// ok lets get some posts from today and render a list | |
$posts_day = $pages->find("categories=$category_page, template=posts, publish_from=$day"); | |
if(count($posts_day)){ | |
foreach($posts_day as $post) { | |
echo "<p>$post->title</p>"; | |
// whatever you wish to output | |
// .. | |
} | |
} else { | |
echo "no posts found for today."; | |
} | |
/** | |
* Now we construct the prev and next day links, if posts were found | |
* --------------------------------------------------------------------------- | |
*/ | |
// get timestamps for use in selectors | |
$prevday = strtotime( "$day -1 day" ); | |
$nextday = strtotime( "$day +1 day" ); | |
// ok lets search some posts but with $pages->count(selector) | |
// sicne we only want to know if any found this is much faster query | |
$posts_preday = $pages->count("categories=$category_page, template=posts, publish_from=$prevday"); | |
$posts_nextday = $pages->count("categories=$category_page, template=posts, publish_from=$nextday"); | |
// convert back date to string for url segment | |
$prevday = date("d-m-Y",$prevday); | |
$nextday = date("d-m-Y",$nextday); | |
// print out prev and next url links | |
if($posts_preday) { | |
echo "<a href='{$config->urls->root}posts/$category/$prevday/'>Prev Day</a> "; | |
} | |
if($posts_nextday) { | |
echo "<a href='{$config->urls->root}posts/$category/$nextday/'>Next Day</a> "; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment