Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.