Created
March 1, 2013 23:29
-
-
Save somatonic/5068777 to your computer and use it in GitHub Desktop.
This script generates a list of post with a prev next pagination to show posts of a specified day and category through urls segments
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 | |
// include("./head.inc"); | |
/** | |
* Script Example | |
* | |
* This script generates a prev next pagination to show posts per day of a specified date | |
* The category is set by the first url segment the date in the second | |
* | |
* as per http://processwire.com/talk/topic/2928-pagination-by-date/ | |
* | |
* - post template has a date field "publish_from" | |
* - post template has a page field "categories" to select category pages | |
* - posts template has urlSegments enabled | |
* - category template with only a title field | |
* | |
* urls for posts for a day by a category: | |
* /posts/[category]/[dd-mm-yyyy]/ | |
* | |
* or default today posts by a category: | |
* /post/[category]/ | |
* | |
* | |
*/ | |
// get category url segment and page | |
if($input->urlSegment1){ | |
$cat = $sanitizer->pageName($input->urlSegment1); | |
} else { | |
$cat = "category1"; // default | |
} | |
$category_page = $pages->get("template=category, name=$cat"); | |
// throw 404 when category not found | |
if(!$category_page->id) { | |
throw new Wire404Exception("Page Not Found"); | |
} | |
// get date url segment | |
if($input->urlSegment2) { | |
$day = $sanitizer->pageName($input->urlSegment2); | |
} else { | |
$day = date("d-m-Y", strtotime("today")); // default | |
} | |
// get post from the day x, to cover 24 hours we need a start and end time | |
$day_end = strtotime( $day . " 23:59:59" ); | |
$posts_day = $pages->find("template=post, categories=$category_page, sort=-publish_date, publish_date>=$day, publish_date<=$day_end"); | |
if(count($posts_day)){ | |
echo "<h2>Posts from day $day</h2>"; | |
echo "<ul>"; | |
foreach($posts_day as $post) { | |
echo "<li><a href='$post->url'>$post->title</a></li>"; | |
} | |
echo "</ul>"; | |
} | |
// look for the next post in future or past | |
$post_prev = $pages->get("template=post, sort=-publish_date, publish_date<=" . strtotime("$day -1 second")); | |
$post_next = $pages->get("template=post, sort=publish_date, publish_date>=" . strtotime("$day +1 day")); | |
// only if posts found we output prev and/or next link | |
if($post_prev->id) { | |
$prev_day = date("d-m-Y", strtotime($post_prev->publish_date)); | |
echo "<a href='{$config->urls->root}posts/$cat/$prev_day/'>prev</a> "; | |
} | |
if($post_next->id) { | |
$next_day = date("d-m-Y", strtotime($post_next->publish_date)); | |
if($post_prev->id) echo " | "; | |
echo "<a href='{$config->urls->root}posts/$cat/$next_day/'>next</a><br/>"; | |
} | |
// include("./foot.inc"); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment