The simple monthly archive

Last modified: June 22, 2009 - 00:24

With Views, it's easy to do a 'Monthly Archive'. This view creates a block which will contain a list of months including the number of posts per month; if there are enough months it will contain a [more] link. Each link goes to a simple teaser view for that month.

Import this view:

<?php
$view
= new stdClass();
 
$view->name = 'archive';
 
$view->description = 'Monthly Archive';
 
$view->access = array (
);
 
$view->view_args_php = '';
 
$view->page = TRUE;
 
$view->page_title = 'Monthly Archive';
 
$view->page_header = '';
 
$view->page_header_format = '1';
 
$view->page_footer = '';
 
$view->page_footer_format = '1';
 
$view->page_empty = '';
 
$view->page_empty_format = '1';
 
$view->page_type = 'teaser';
 
$view->url = 'archive';
 
$view->use_pager = TRUE;
 
$view->nodes_per_page = '10';
 
$view->block = TRUE;
 
$view->block_title = 'Archive';
 
$view->block_header = '';
 
$view->block_header_format = '1';
 
$view->block_footer = '';
 
$view->block_footer_format = '1';
 
$view->block_empty = '';
 
$view->block_empty_format = '1';
 
$view->block_type = 'list';
 
$view->nodes_per_block = '5';
 
$view->block_more = TRUE;
 
$view->block_use_page_header = FALSE;
 
$view->block_use_page_footer = FALSE;
 
$view->block_use_page_empty = FALSE;
 
$view->sort = array (
    array (
     
'tablename' => 'node',
     
'field' => 'created',
     
'sortorder' => 'DESC',
     
'options' => 'normal',
    ),
  );
 
$view->argument = array (
    array (
     
'type' => 'monthyear',
     
'argdefault' => '5',
     
'title' => '%1',
     
'options' => '',
     
'wildcard' => '',
     
'wildcard_substitution' => '',
    ),
  );
 
$view->field = array (
    array (
     
'tablename' => 'node',
     
'field' => 'title',
     
'label' => '',
     
'handler' => 'views_handler_field_nodelink',
     
'options' => 'link',
    ),
  );
 
$view->filter = array (
  );
 
$view->exposed_filter = array (
  );
 
$view->requires = array(node);
 
$views[$view->name] = $view;
?>

And use this code in your template.php (You may have to adjust this slightly if using a non-phptemplate theme). This theme will make the summary look more like a menu.

<?php
function phptemplate_views_summary_archive($view, $type, $level, $nodes, $args) {
  foreach (
$nodes as $node) {
   
$list .= '<li class="leaf">' . views_get_summary_link($view->argument[$level]['type'], $node, $view->real_url) . "</li>\n";
  }

  if (
$list) {
    return
"<div class='menu'><ul>$list</ul></div>";
  }
}
?>

Some Notes:

  1. To prevent that unpublished nodes or non-blog nodes are taken into account, the view should also filter on:
    Node: Published = Yes
    Node: Type = One of 'Blog entry'
  2. Archive as a Drop Down List

    We modified the function to draw the archive list as a drop down, and works sweet.

    We still havent managed to find how to get the actual url that each entry should point to into the VALUE='' field ... so if anyone knows we would appreciate the addition to the page...

    eg. <OPTION VALUE='archive/200803'>

    1. <?php
      function phptemplate_views_summary_archive($view, $type, $level, $nodes, $args) {
       
      $count = 1;
        foreach (
      $nodes as $node) {
         
      $list .= "<OPTION VALUE='".$count."'>". views_get_summary_link($view->argument[$level]['type'], $node, $view->real_url) . ': ' . $node->num_nodes . ' items';
        
      $count += 1;
        }

        if (
      $list) {
          return
      "<div class='menu'><FORM NAME='myform' onChange='followlink()'><SELECT NAME='mylist'>$list</SELECT></FORM></div>";
        }
      }
      ?>

      The Drupal 6 version of Views comes with a default view that achieves this functionality.

 
 

Drupal is a registered trademark of Dries Buytaert.