Croogo Monthly Archives
Introduction
Most popular blogging engines usually have some way to categorize articles by month. This short code snippet will allow you to display monthly archives like those blogging engines. I'm assuming that Croogo will eventually add this to the core but for now here is a simple solution.
Requirements
- Croogo 1.1
- MySQL
Installation
You must include the two actions into your nodes controller (archives and get_archives) and make sure that you enable them for public access under Users > Permissions in the administration panel. Also included is an element (archives.ctp) that must be placed into your themes element folder. Once you have placed the element into the correct directory go back into your administration panel and then go to Blocks > Blocks and add a new block under the "right" region and then in the elements text input type "archives". This will include the archives.ctp in the right region.
Code
<?php
public function get_archives () {
$this->autoRender = false;
$archives = $this->Node->query(
"SELECT MONTH(created) AS month,
YEAR(created) AS year
FROM nodes
WHERE type = 'blog'
AND status = 1
GROUP BY MONTH(created)
ORDER BY DATE(created) DESC"
);
return $archives;
}
public function archives () {
$this->paginate['Node']['order'] = 'Node.id DESC';
$this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
$this->paginate['Node']['conditions'] = array(
'Node.status' => 1,
'Node.promote' => 1,
'MONTH(Node.created)' => $this->params['month'],
'YEAR(Node.created)' => $this->params['year']
);
$this->paginate['Node']['contain'] = array(
'User',
'Meta',
'Comment',
'Term' => array('Vocabulary')
);
if (isset($this->params['named']['type'])) {
$type = $this->Node->Term->Vocabulary->Type->findByAlias($this->params['named']['type']);
if (!isset($type['Type']['id'])) {
$this->Session->setFlash(__('Invalid content type.', true));
$this->redirect('/');
exit();
}
$this->paginate['Node']['conditions']['Node.type'] = $type['Type']['alias'];
$this->pageTitle = $type['Type']['title'];
$this->set(compact('type'));
}
$nodes = $this->paginate();
$this->set(compact('nodes'));
$month = $this->params['month'];
$year = $this->params['year'];
$this->set(compact('month', 'year'));
$this->set('title', get_month($month) . ' ' . $year);
}
?>
If you have any questions or comments drop me a line below and I will try to get back to you as soon as possible.
Last Updated: December 9th 2009, 6:27am
Download: Croogo Monthly Archives


3 Comments
get_month function is missing. You can just use something like below to convert month to year:
date('F', mktime(0, 0, 0, $month[1]))
Also, your query in it's current state will group months together - which isn't what we want. For example.. if you have some posts in December 2008, and some posts in December 2009 - it's going to group them together. To get around that, just change the MONTH(created) as month to DATE_FORMAT(created, '%Y/%m') AS month
Then, in your view foreach, you can use explode on the month to get the correct digits:
$month = explode('/', $archive[0]['month']);
That should fix a few of the issues I found so far with this.
Good job!