Question

I need to get something like this:

2014

  • post 1
  • post 2
  • post 3
  • post 4

2013

  • post 1
  • post 2
  • post 3

How can I achieve this?

No correct solution

OTHER TIPS

Here is what you need:

$yearly = new WP_Query(array('posts_per_page' => -1));
$prev_year = null;
if( $yearly->have_posts() ) : while( $yearly->have_posts() ) : $yearly->the_post();
      $this_year = get_the_date('Y');
      if ($prev_year != $this_year) {
          if (!is_null($prev_year)) {
             echo '</ul>';
          }
          echo '<h3>' . $this_year . '</h3>';
          echo '<ul>';
      }
      echo '<li>';
      echo '</li>';
      $prev_year = $this_year;
    endwhile;    
    echo '</ul>';
endif;

Please take a look at the documentation as well: http://codex.wordpress.org/Class_Reference/WP_Query

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top