Jump to content

Recommended Posts

Posted

I'm using the getArchives() and renderArchives() functions found in the /templates/archives.php file from the Blog Profile developed by Ryan. These functions are used independently of the blog profile and have been added to a site that has already been developed in PW.

The year/month archive listing in the sidebar work fine on all blog pages expect for pages that are paginated. I'm thinking that the additional URL segment is throwing things off, but that's just a guess.

Has anyone had any experience with displaying a date-based archive listing of blog posts? The listing needs to be structured like this:

2011
  > January (1)
2010
  > June (2)
  > July (1)
  > November (1)
2009
  > November (1)
2008
  > February (1)
2006
  > January (1)
Posted

Without knowing the full details of your context, you mentioned you are using pagination: I would suggest checking if you need to add a "start=0" to your selector that retrieves the listing of pages for your sidebar. 

Posted

@ryan,

Sorry, I should have provided the code that I am using from your Blog site profile. I'm not sure where I would add the "start=0."

function getArchives( $year = 0, $limit = 0 ) {
    if ( $year ) {
        $firstYear = $year;
        $lastYear = $year;
    } else {
        $oldest = wire('pages')->get( "template=post, date>0, sort=date" );
        $newest = wire('pages')->get( "template=post, date>0, sort=-date" );
        if ( !$newest->id ) return '';
        $firstYear = date('Y', $oldest->getUnformatted('date'));
        $lastYear = date('Y', $newest->getUnformatted('date'));
    }
    $_limit = $limit > 1 ? (int) $limit : 2;
    $years = array();
    for ( $y = $lastYear; $y >= $firstYear; $y-- ) {
        $months = array();
        $numPostsYear = 0;
        for ( $month = 1; $month <= 12; $month++ ) {
            $firstDay = strtotime( "$y-$month-01" );
            $lastDay = strtotime( "+1 month", $firstDay ) - 1;
            $posts = wire('pages')->find( "template=post, date>=$firstDay, date<=$lastDay, limit=$_limit, sort=-date" );
            $numPosts = $posts->getTotal();
            if ( !$numPosts ) continue;
            $numPostsYear += $numPosts;
            $months[$month] = array(
                'url' => wire('config')->urls->root . "blog/archive/$y/$month/",
                'name' => strftime('%B', $firstDay),
                'posts' => $limit > 0 ? $posts : array(),
                'total' => $numPosts
                );
        }
        if(!$numPostsYear) continue;
        $years[$y] = array(
            'url' => wire('config')->urls->root . "blog/archive/$y/",
            'name' => $y,
            'total' => $numPostsYear,
            'months' => $months
            );
    }
    return $years;
}
function renderArchives(array $years) {
    $out = '';
    foreach ( $years as $year ) {
        $t = new TemplateFile(wire('config')->paths->templates . 'includes/archives.php');
        $t->set('year', $year['name']);
        $t->set('total', $year['total']);
        $t->set('months', $year['months']);
        $t->set('url', $year['url']);
        $out .= $t->render();
    }
    return $out;
}
$archives = renderArchives(getArchives());
Posted

You would want to include a "start=0" in a find() call that has a "limit=n" in its selector. This would be only for the results you don't want to paginate. I'm thinking this is the line: 

$posts = wire('pages')->find( "template=post, date>=$firstDay, date<=$lastDay, limit=$_limit, sort=-date, start=0" );
  • 3 weeks 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
×
×
  • Create New...