Last updated June 22, 2009. Created on March 2, 2006.
Edited by bryan kennedy, Wolfflow, birdmanx35, add1sun. Log in to edit this page.

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.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

fehin’s picture

My url looks like this: user/7/blog/archive/2009/200901
My arguments are in this order:

User: UID is Author
Node: Posted Year
Node: Posted Month + Year

and I used this php argument

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) { // check whether its a node or not

$node=node_load(arg(1)); // if its a node
 
$args[0] = $node->uid;
}
else {

// if no node, it have to be the blog homepage so UID is arg(1) (blog/UID)
// arg(1) is the UID of my blog coz my url alias blog/[UID]
// if you have another link that includes the UID, just change arg(1) to your UID arg

 
$args[0] = arg(1);
  }

return
$args;
?>

which I got it from here: http://drupal.org/node/231038

OnthegOinOz’s picture

Thanks for putting this up... I have tried this on Drupal 6 without any luck and notice the reference to a version of it at the bottom of the original post, but not link to a drupal 6 version?? Could you please post this version as well?

thanks

Summit’s picture

Subscribing, interesting in D6 version also.

Edit: isn't this the D6 version: http://drupal.org/node/262399

Yes it is see http://www.hirsi.nl left under menu, nice archive!

Greetings, Martijn

sebasje83’s picture

maybe one of you can help me out on the following matter. I have a blog and a monthly archive installed. The monthly archive works fine but what I would like to see is that each month my blog page empties and the previous months blog posts are in the archive.. I also tried schedulars but when unpublishing the archive doesnt show the entries anymore. Hope to hear from you soon. Thanks in advance

truyenle’s picture

Hi Everyone,

I enable the view called archive go with views 2. Work great. I can now archive it by month for a certain content type by adding node:type to filter.

However, the path to archive page is always archive/yyyymm. I really want to change it to blog/yyyymm but when I change it under page display's path of this views => The links in the archive block will link to page not found?

Any idea how to customize this path?

Well! Just now I got it. Instead of place abc as a replacement of archive I have to use "%" in the URL to represent values that will be used for arguments. So place blog/%.

it will work like charm.

Thanks
Truyenle

jjjames’s picture

Subscribing, good find!

davethedruper’s picture

Thank you for creating this views as it is great! Just wondering how one would create a panels for the archived pages? I've tried just about everything but haven't been able to get the pages into a panel. I'm assuming it's some kind of argument I'm missing in the panels page. Please help if anyone knows how to accomplish this addition.

Thanks in advance.

dunklea’s picture

I would like this functionality as well.

mgifford’s picture

Start with the Archive view that comes built into Views 3.x.