Jump to content

Creating archives for news/blogs sections


ffub
 Share

Recommended Posts

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:

323149691-28f3ce7f9840fc265dec4422a1655329.4dfa2f88-full.png

Many thanks,

Stephen

Link to comment
Share on other sites

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. :)

  • Like 3
Link to comment
Share on other sites

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
   }
}

  • Like 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 years later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...