Creating a monthly archive list
From PixyWiki
This article is adapted from this post by Wayne Horkan.
Many blogs display a list of months that when clicked show the posted entries for that month.
Here's an example using our News Blog:
- » June, 2009
- » May, 2009
- » April, 2009
- » March, 2009
- » February, 2009
- » January, 2009
- » December, 2008
This can be achieved using the following velocity code:
## Blog Start Year
#set ($startYear = 2006)
## Blog Start Month
#set ($startMonth = 5)
## Get the current Year and Month
#set ($countYear = $utils.now.getYear() + 1900)
#set ($countMonth = $utils.now.getMonth() + 1)
## Text Adjust
#set ($zero = 0)
## Total number of years to cycle through
#set ($numberYear = $countYear - $startYear)
<ul class="rEntriesList">
#foreach ($number in [0..$numberYear])
#if ($countYear >= $startYear)
## Months in a year
#foreach ($number in [1..12])
#if (($countMonth != 0) && ((($countYear > $startYear) || (($countYear == $startYear) && ($countMonth >= $startMonth)))))
<li class="recentposts">
#if ($countMonth == 1)
#set ($nameMonth = "January")
#elseif ($countMonth == 2)
#set ($nameMonth = "February")
#elseif ($countMonth == 3)
#set ($nameMonth = "March")
#elseif ($countMonth == 4)
#set ($nameMonth = "April")
#elseif ($countMonth == 5)
#set ($nameMonth = "May")
#elseif ($countMonth == 6)
#set ($nameMonth = "June")
#elseif ($countMonth == 7)
#set ($nameMonth = "July")
#elseif ($countMonth == 8)
#set ($nameMonth = "August")
#elseif ($countMonth == 9)
#set ($nameMonth = "September")
#elseif ($countMonth == 10)
#set ($nameMonth = "October")
#elseif ($countMonth == 11)
#set ($nameMonth = "November")
#elseif ($countMonth == 12)
#set ($nameMonth = "December")
#end
#if ($countMonth > 9)
<a href="$url.page("Blog")?date=$countYear$countMonth">» $nameMonth, $countYear</a>
#else
<a href="$url.page("Blog")?date=$countYear$zero$countMonth">» $nameMonth, $countYear</a>
#end
</li>
#set ($countMonth = $countMonth - 1)
#end
#end
#set ($countMonth = 12)
##</ul>
#end
#set ($countYear = $countYear - 1)
#end
Note: change the values of $startYear and $startMonth to reflect that of your blog.


