Creative Commons License
This weblog is licensed under a Creative Commons License.

« Damn! | Main | The Unicorn »

Flexible Movable Type Archive Sidebar

Its been a while since a detailed nerdy posting, so I figured it was about time. As my boy's blog has gotten longer (and this one too), I began to dislike how long the archive listing got on the side. I wanted to be able to collapse years together, and after about an hour and half of screwing around this afternoon, I got it to work - code follows the break. You can see it working live on this site - all the entries for 2005 are grouped together in the Archive section of the sidebar.

These style tags need to go into the page stylesheet:

.thinghidden {display:none;}
.thingshown {display:inline;}

This script needs to be included in the page that gets built:

<script>
function expandcollapse (thing)
{
    whichthing = document.getElementById(thing);
    if (whichthing.className=="thingshown")
    {
        whichthing.className="thinghidden";
    }
    else
    {
        whichthing.className="thingshown";
    }
}
</script>

This PHP builds the archive list for the sidebar. It goes inside the div where the archive tags normally sit.

<?php
//
// build the array
//
$index=0;
<MTArchiveList archive_type="Monthly">
$date[$index] = "<$MTArchiveTitle$>";
$link[$index] = "<$MTArchiveLink$>";
$index=$index+1;
</MTArchiveList>

//
// run though each month, inserting year breaks where appropriate
//
$len = strlen( $date[0] );
$baseyear = substr( $date[0], $len-4 );
$hasspan = 0;
echo "<ul class='module-list'>";
for( $i=0;$i<$index;$i++ )
{
    //
    // if the year rolls over, insert a year tag and a hidden span
    //
    $year = substr( $date[$i], strlen($date[$i]) - 4 );
    if( $year != $baseyear )
    {
        echo "</ul>";
        if( $hasspan ) echo "</span>";
        echo "<b><a href=\"javascript:expandcollapse('MoreArchive$year')\">$year</a></b><br/>";
        echo "<span id='MoreArchive$year' class='thinghidden'><br/><ul class='module-list'>";
        $hasspan = 1;
        $baseyear = $year;
    }

    //
    // add a list item for this month
    //
    echo "<li class='module-list-item'><a href='$link[$i]'>$date[$i]</a></li>";
}

//
// closeout open tags
//
echo "</ul>";
if( $hasspan) echo "</span>";
?>