Support » Fixing WordPress » Nesting months under years in archives

  • Resolved peterpunzi


    Hello All,
    I have been trying to figure this out for a few days and perhaps I am not using the right search terms for the support forum to find the answer. The standard format for the archives is to make an unordered list for each and every month I would like to be able to have the archives format so that the months are nested under each year. For example:

    <li>Month 1</li>
    <li>Month 2</li>
    <li>Month 3 etc.</li>

    It is the way the pages and the categories are formatted.

    I apologize if this is a reoccuring question but I did try to solve this using search and looking at all the archive plug-ins.
    Thanks for any assistance you can provide.


Viewing 3 replies - 1 through 3 (of 3 total)
  • I forgot to mention that I did see this in the archive plugin:

    CG-Archives by Year
    Groups archives by the year and sublists the months (written in response to this thread (

    But the link is dead.

    Thanks again,

    Once again I have found my own solution so I will post it so that others may find it. Since CG-Archives by Year
    was gone I tried seaching for a cache of the website. Although Google did not have one I did find a text file cached on Yahoo and sure enough it was what I needed to make the plugin! So since the link is dead on the Plugin page I am going to list post the file here. All the directions are included in the text (although it is a bit cryptic – reading it a few times helps). Just copy the following text into notepad and save as cg_archives-by-year-3.3.php (make sure there are now spaces before or after the opening and closing php brackets). Then upload to your Plugins directory and follow the directions in the file. Here it is:

    Plugin Name: CG-Archives by Year
    Plugin URI:
    Description: Gets the archive list in a yearly format so that users can split up the goods.
    Version: 3.3
    Author: Chuck G.
    Author URI:

    Plugin Note:
    It has since then been implemented as a "plugin of sorts". All that means is that to use it now, you
    must put this file in the wp-content/plugins directory, activate the file, and use the cg_get_archives_by_year()
    function accordingly. The directions for doing so are listed below ....

    Basic Notes:
    Ok so this was written in response to this post at the WordPress Support forum:

    It was only tested on my side of things, so it may or may not work properly on your setup.
    But the good news is that I think it'll work on everything. If you do have problems, feel
    free to email me: chuckg @

    What these two functions attempt to do is create an archive link structure that is based
    upon the assumption that for some years, the author might or might not want the months listed
    based on whether it is the current year or not. So, if the current year is 2004, the author would
    want the month links shown, but if we are looking at the previous years, the author would not want
    the links for months.

    The first number, $limit, indicates the total number of links you would like shown. It's hard to explain
    how this variable works other than it limits the results of the query to the database; ie: it returns
    the results of the months you would like listed _plus_ the years you want listed. The plugin will only
    show the month links for the current year if you use $month_limit, the limit function is really only useful
    unless you want months from previous years listed as well. If you're using the plugin to limit the previous
    years month links, set $limit to something around 24.

    The second number is the "month_limit", or how many month links you would like listed in the current year.
    To list the months for the entire year, you would set this to say, 12 (how many months there are out of the year).
    Or if you only wanted the first 2 month shown, you would set this to two and so on. I recommend setting this
    to 12 for the most clear results. If you would like months shown in the previous years, simply leave the
    $month_limit blank or set it to 0.

    Now, to get the links to come out the way you want...throw something like this into your code...
    cg_get_archives_by_year('24', '4', '<li>', '</li>', '<ul>', '</ul>', '<li>', '</li>', true);

    This would produce something along the lines of:
    (assuming the year is 2004, and you really have posts in the months I'm listing)
    January (4)
    February (5)
    October (2)
    May (4)

    I feel I should further explain a few of the variables, namely the $open_month and $close_month variables. This is
    the main distinction between this version of this "plugin" and the original hack. I've changed the way the markup is
    handled, so let me explain. Previously, the entire list was encompassed within one large <ul> tag and months were
    simply classified differently to create the tabulated effect.

    To better handle "strict markup" and better css effects, I've modified this process a bit. Now, each year and it's preceeding
    months will be within *one* <li> tag. The year will be simply printed within the <li>, but the months for that year will then
    be inserted within a *nested* <ul> tag. Let me illustrate the (in psuedo code) what the above example will now look like:

    <!--opens 2004-->
    <li><a href="#" title"January">January</a> <a href="#" "January Post Count">(4)</a></li>
    <li><a href="#" title"February">February</a> <a href="#" "February Post Count">(4)</a></li>
    <!--closes 2004-->

    I didn't feel inclined to write out *all* of the code, but I believe you get the gist of it. The reason for this change is it
    allows for easier manipulation of the months. If need an example of what I mean, check out my archives:
    The example is actually the on the side bar, not the main area. The main "archive" is a whole other thing entirely.



    function cg_get_archives_by_year ($limit='24', $month_limit='12', $before_year='', $after_year='', $open_month='', $close_month='', $before_month='', $after_month='', $show_post_count = false) {
    global $tableposts, $dateformat, $time_difference, $siteurl, $blogfilename;
    global $querystring_start, $querystring_equal, $querystring_separator, $month, $wpdb;

    // A little formatting for the ul structure;
    $before_year = "nt" . $before_year;
    $after_year = "nt" . $after_year . "n";
    $open_month = "t" . $open_month . "n";
    $close_month = "t" . $close_month;
    $before_month = "tt" . $before_month;
    $after_month = $after_month;

    if ('' != $limit) {
    $limit = (int) $limit;
    $limit = " LIMIT $limit";

    // Set our month_limit to null so we don't have to do checks for 0 or empty in all of our
    // if statements. This has been a pain in my ass from the start.
    if ($month_limit == '' || $month_limit == "0") {
    $month_limit = null;

    // this is what will separate dates on weekly archive links
    $archive_week_separator = '–';

    // archive link url
    $archive_link_m = $siteurl.'/'.$blogfilename.$querystring_start.'m'.$querystring_equal; # monthly archive;
    $archive_link_y = $siteurl.'/'.$blogfilename.$querystring_start.'year'.$querystring_equal;

    // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
    $archive_date_format_over_ride = 0;

    // options for daily archive (only if you over-ride the general date format)
    $archive_day_date_format = 'Y/m/d';

    // options for weekly archive (only if you over-ride the general date format)
    $archive_week_start_date_format = 'Y/m/d';
    $archive_week_end_date_format = 'Y/m/d';

    if (!$archive_date_format_over_ride) {
    $archive_day_date_format = $dateformat;
    $archive_week_start_date_format = $dateformat;
    $archive_week_end_date_format = $dateformat;

    $now = date('Y-m-d H:i:s',(time() + ($time_difference * 3600)));
    $thisyear = date('Y');

    $arcsql = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM $tableposts WHERE post_date < '$now' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC" . $limit;
    $arcresults = $wpdb->get_results($arcsql);
    if ($arcresults) {
    $arccount = count($arcresults); // Count of results from SQL.
    $loopcount = 0; // Total iterations through the program.
    $monthcount = 0; // Keep track of the total months.

    foreach ($arcresults as $arcresult) {
    // Do our loopcount up here

    $currYear = $arcresult->year;
    $y_url = get_year_link($arcresult->year);
    $m_url = get_month_link($arcresult->year, $arcresult->month);
    if ( $prevYear != $currYear ) {
    $y_text = $currYear;

    if ($loopcount != 1) {
    if (($month_limit > 0 && $thisyear == $prevYear) || !$month_limit ) {
    echo $close_month;
    echo $after_year;

    echo $before_year . '<a href="'. get_year_link($currYear) .'" title="Archives: '. $currYear .'">'. $currYear .'</a>' . "n";
    if (($month_limit > 0 && $thisyear == $currYear) || !$month_limit ) {
    echo $open_month;

    // Setup the month text stuff.
    if ($show_post_count) {
    $m_text = sprintf("%s", $month[zeroise($arcresult->month,2)]);
    $m_after = "&nbsp;($arcresult->posts)$after_month";
    } else {
    $m_text = sprintf("%s", $month[zeroise($arcresult->month,2)]);

    // Using the month_limit feature.
    if ($monthcount != $month_limit && $thisyear == $currYear) {
    echo get_archives_link($m_url, $m_text, "custom" , $before_month, $m_after);
    } elseif (!$month_limit) { // The total limit feature.
    echo get_archives_link($m_url, $m_text, "custom" , $before_month, $m_after);

    if ($loopcount == $arccount) {
    if (!$month_limit) {
    echo $close_month;
    echo "<!--test-->";
    echo $after_year;

    $prevYear = $currYear;

    if (!function_exists('get_year_link')) {
    function get_year_link($year) {
    global $siteurl, $blogfilename, $querystring_start, $querystring_equal;
    if (!$year) $year = date('Y', time()+($time_difference * 3600));
    if ('' != get_settings('permalink_structure')) {
    $off = strpos(get_settings('permalink_structure'), '%year%');
    $offset = $off + 7;
    $yearlink = substr(get_settings('permalink_structure'), 0, $offset);
    $yearlink = str_replace('%year%', $year, $yearlink);
    return get_settings('home').$yearlink;
    } else {
    return get_settings('home').'/'.get_settings('blogfilename').$querystring_start.'year'.$querystring_equal.$year;

    Hi Peter.

    Thanks for going through the effort to find this. I’m looking around high and low for the plugin too. 🙂


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Nesting months under years in archives’ is closed to new replies.