WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I thought this would be simpler using the basic widgets that WP provides but its not.

I need to create

  • 2012
    • Decemeber
    • November
    • ...
  • 2011
    • December
    • ...

The month items would be links to that archive.

Is this possible? I would prefer not to use a plugin but if thats the answer its ok.

share|improve this question
up vote 3 down vote accepted
function archive_links_custom() {
    global $wpdb, $wp_locale;
    $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
    $arcresults = $wpdb->get_results($query);
    if ( $arcresults ) {
        $year = null;
        foreach ( (array) $arcresults as $arcresult ) {
            if($arcresult->year != $year) {
                echo $arcresult->year;
                $year = $arcresult->year;
            $url = get_month_link( $arcresult->year, $arcresult->month );
            $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month($arcresult->month), $arcresult->year);
            $after = ' ('.$arcresult->posts.')';
            echo '<a href="'.$url.'>'.$text.$after.'</a>';

This function should do what you want. Customize the markup in there the way you like

share|improve this answer
This looks right. How would I call this in my sidebar.php? – TJ Sherrill Oct 22 '12 at 17:35
put the function in functions.php & just make the call directly. or if you like, copy the function content to sidebar.php – Mridul Aggarwal Oct 22 '12 at 19:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.