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.

1 Answer 1

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

  • This looks right. How would I call this in my sidebar.php? Oct 22, 2012 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 Oct 22, 2012 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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