project: Monthly Blog Archive

photo Jeff Schuler
Cleveland, OH, 44113 United States
+1-585-329-8186 (c)

Dynamically create a blog archive page with yearly and monthly separations, using php and MovableType archive-generation.

Thanks to Anil Dash's My Favorite View of My Blog for the formatting idea.

monthly_archive_linklist.tmp
(a MovableType template,) generates a php file, which stores to a variable ($bigstring) an HTML list of links to all monthly archive pages. MovableType rebuilds this file whenever a new monthly archive page is created.

  <?php
$bigstring
='<MTArchiveList archive_type="Monthly">
<a href="<$MTArchiveLink$>"><$MTArchiveTitle$></a> (<$MTArchiveCount$>)<br />
</MTArchiveList>'
;
?>
monthly_archive_transform.php
(a php/html file,) includes the one MovableType generates (with monthly_archive_linklist.tmp) to set $bigstring, which is parsed and reformatted.

<?php
include('/home/clevelb3/public_html/jeffschuler/archive/monthly_archive_linklist.php'); #sets $bigstring
$lines explode("\n"$bigstring);
$curYear 0;
print 
"<table><tr>\n";
foreach(
$lines as $string){
        
$pattern '/\>(.*) (.*)\<\/a\>/';
        
$replacement '\>$1\</a\>';
        
preg_match($pattern$string$matches);
        if(
$matches){
                
$monthAbbrevArray str_split($matches[1], 3);
                
$monthAbbrev $monthAbbrevArray[0];
                
$year $matches[2];
                if(
$year != $curYear){
                        if(
$curYear != 0){print "</td>\n";}
                        print 
"<td valign=\"top\">\n<p><strong>$year</strong></p>\n<p>";
                        
$curYear $year;
                }
                
$pattern '/\>.*\<\/a\>/';
                
$replacement ">$monthAbbrev</a>";
                
$newstring preg_replace($pattern$replacement$string);
                print 
"$newstring\n";
        }
}
print 
"</tr></table>\n";
?>

The end result is shown in my blog archive by date page.