ffub Posted June 16, 2011 Posted June 16, 2011 Hi, I'm loving ProcessWire and the freedom it allows. Most of my sites are fairly hierarchical and a great fit for the tree model of ProcessWire (which I tend to think of like the DOM in HTML). Most of my sites also feature a blog or news section though and it's these bucket-like sections I'm struggling with. I tend to just place all the entries under a page, and use index and story templates to display them. In a previous topic you describe a way of filtering by date or another field. I would like however to create some indexes of these fields themselves rather than listing the actual posts. This would be very useful for creating an archives page, a list of tags or some archive links in the sidebar but I'm not sure how to query the tree in this manner. For example, how would I go about creating the following: Many thanks, Stephen
ryan Posted June 17, 2011 Posted June 17, 2011 Hi Stephen, Welcome to the forums and good question. Here's one way you could approach it, assuming all those posts were under the same page (like /blog/). I'm somewhat limited on time right now, so this is written in the browser and not tested, but I can follow up with more detail later if you'd like. <?php $blog = $pages->get("/blog/"); $startYear = date("Y"); // this year $endYear = 2009; // or whenever you want it to end $now = time(); for($year = $startYear; $year >= $endYear; $year--) { for($month = 12; $month > 0; $month--) { $startTime = strtotime("$year-$month-01"); // 2011-12-01 example if($startTime > $now) continue; // don't bother with future dates if($month == 12) $endTime = strtotime(($year+1) . "-01-01"); else $endTime = strtotime("$year-" . ($month+1) . "-01"); $entries = $blog->children("created>=$starttime, created<$endTime"); // or substitute your own date field $date = date("m-Y"); echo "<li><a href='./$date'>$date - " . count($entries) . "</a></li>"; // output the month and count } } Then you'd use the same template to list the entries when a $input->urlSegment1 has a date specified in it. Basically you'd cycle through the $entries found above and link to them. This is rough and just approximate. I'll be glad to follow up with more detail... but wife is telling me to get off the computer. 3
ffub Posted June 17, 2011 Author Posted June 17, 2011 Many thanks for your swift and detailed reply , Ryan. With a couple of minor changes your code works fine. I have to check the count was above 0, and pass the starttime into $date. This is what worked for me: <?php $channel = $pages->get("/news/"); $startYear = date("Y"); // this year $endYear = 2009; // or whenever you want it to end $now = time(); for($year = $startYear; $year >= $endYear; $year--) { for($month = 12; $month > 0; $month--) { $startTime = strtotime("$year-$month-01"); // 2011-12-01 example if($startTime > $now) continue; // don't bother with future dates if($month == 12) $endTime = strtotime(($year+1) . "-01-01"); else $endTime = strtotime("$year-" . ($month+1) . "-01"); $entries = $channel->children("created>=$startTime, created<$endTime"); // or substitute your own date field $date = date("F, Y",$startTime); $url = "/news/" . date("Y",$startTime) . "/" . date("m",$startTime); $count = count($entries); if($count > 0) echo "<li><a href='$url'>$date <b>" . $count . "</b></a></li>"; // output the month and count } } 1
ryan Posted June 20, 2011 Posted June 20, 2011 Great–glad that worked with minor changes. Thanks for the follow-up. I'm thinking we should adjust the date-based selector strings to automatically run strtotime on any strings passed to it that aren't all digits. That way we wouldn't have to use strtotime to build the selector in instances like this.
melissa_boyle Posted January 16, 2014 Posted January 16, 2014 Hi Guys, I have made use of this code and it works fab, however im unsure of how to list the entries for each month/year. I am still pretty new to PW, any help would be greatly appreciated. Thanks, Melissa
Martijn Geerts Posted January 17, 2014 Posted January 17, 2014 An other way could be using the module Date Archiver from u-nikos. It will do the archiving, list /year/month/day/ automagically.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now