Archive Template in WordPress

I want to create an archive page template for WordPress that will look like this:

August 2009

Read More
  • Post 4
  • Post 3
  • Post 2
  • Post 1

July 2009

  • Post 2
  • Post 1

So, basically, I want all the posts from the blog, ordered descending by date and grouped by month.
Can someone provide me the PHP code for this?

Thanks!

PS: WordPress version is 2.8.2

Related posts

Leave a Reply

3 comments

  1. This is a function I created a while back. It basically does what you want to do, but it’s not a template. Maybe you can adapt it.

    <?php
    /**
     * Displays a condensed list of the posts grouped by month/year. 
     *
     * @param $order The order of the posts.  Either 'DESC' or 'ASC', case sensitive.
     * @param $date_prefix Whether to prefix the posts with the month/date.
     * @param $display Whether to display the results or return it as a String. 
     */
    
    function condensed_post_list($order='DESC', $date_prefix=true, $display=true){
        global $wpdb;
    
        if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
        $query = "SELECT ID, post_title, post_date FROM $wpdb->posts ".
                 "WHERE post_type='post' AND post_status = 'publish' ".
                 "ORDER BY post_date $order";
        $results = $wpdb->get_results( $query );
    
        ob_start();
        $current_month = '';
        foreach( $results as $result ) {
            if( $current_month != mysql2date('F Y', $result->post_date)) {
                if( $current_month ) echo '</ul>';
    
                $current_month = mysql2date('F Y', $result->post_date );
                echo '<h2>'.$current_month.'</h2>';
                echo '<ul>';
            }
            echo '<li>';
            echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
            echo '<a href="'.get_permalink($result->ID).'">';
            echo $result->post_title.'</a></li>';
        }
        if( $current_month ) echo '</ul>';
    
        if( $display ) {
            ob_end_flush();
        } else {
            return ob_get_clean();
        }
    }
    ?>
    
  2. I used the above function, but replaced the SQL query with:

    $results = query_posts('post_type=post&post_status=publish&cat=3');
    

    This allowed me to use the excellent function @scompt.com wrote, but limit it to a single blog category.

  3. Thanks a lot of the help. This is what I used with the code above.

    function condensed_post_list($order='DESC', $date_prefix=true, $display=true)
    {
        if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
        $args = array(
            'numberposts'     => -1,
            'orderby'         => 'post_date',
            'post_type'       => 'post',
            'post_status'     => 'publish');
        $results = get_posts($args);
    
        ob_start();
        $current_month = '';
        foreach( $results as $result ) {
            if( $current_month != mysql2date('F Y', $result->post_date)) {
                if( $current_month ) echo '</ul>';
    
                $current_month = mysql2date('F Y', $result->post_date );
                echo '<h2>'.$current_month.'</h2>';
                echo '<ul>';
            }
            echo '<li>';
            echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
            echo '<a href="'.get_permalink($result->ID).'">';
            echo $result->post_title.'</a></li>';
        }
        if( $current_month ) echo '</ul>';
        if( $display ) {
            ob_end_flush();
        } 
        else {
            return ob_get_clean();
        }
    }