• Hi Guys,

    I would like to have a archive that groups post by year like this:

    2010
    Post 6
    2009
    Post 5
    Post 4
    2008
    Post 3
    Post 2
    Post 1

    Can someone give me a helping hand with the code to do this? I would appreciate it.

Viewing 1 replies (of 1 total)
  • <?php
    $args=array(
        'orderby' => 'date',
        'order' => 'ASC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1
    );
    $oldestpost =  get_posts($args);
    
    $args=array(
        'orderby' => 'date',
        'order' => 'DESC',
        'posts_per_page' => 1,
        'caller_get_posts'=>1
    );
    $newestpost =  get_posts($args);
    
    if ( !empty($oldestpost) && !empty($newestpost) ) {
      $oldest = mysql2date("Y", $oldestpost[0]->post_date);
      $newest = mysql2date("Y", $newestpost[0]->post_date);
    
      for ( $counter = intval($newest); $counter >= intval($oldest); $counter -= 1) {
    
        $args=array(
          'year'     => $counter,
          'posts_per_page' => -1,
          'orderby' => 'date',
          'order' => 'DESC',
          'caller_get_posts'=>1
        );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo '<h2>Posts for ' . $counter . '</h2>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><small><?php the_time('F jS, Y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
            //the_content('Read the rest of this entry &raquo;');
          endwhile;
        } //if ($my_query)
      wp_reset_query();  // Restore global post data stomped by the_post().
      }
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Group posts by year’ is closed to new replies.