Advertisement
Viper007Bond

Untitled

Aug 19th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.15 KB | None | 0 0
  1. <?php
  2.  
  3. // Get the oldest post in this category
  4. // This will tell us how many years we need to go back
  5. $oldest = get_posts( array(
  6.     'category_name'  => get_query_var( 'category_name' ),
  7.     'orberby'        => 'date',
  8.     'order'          => 'ASC',
  9.     'posts_per_page' => 1,
  10. ) );
  11.  
  12. // Build the array of years to show (oldest -> now)
  13. $years = range( get_the_time( 'Y', $oldest->ID ), date( 'Y', current_time( 'timestamp' ) ) );
  14.  
  15. // Loop through the years and output the posts for that year
  16. echo '<ul>';
  17. foreach ( $years as $year ) {
  18.  
  19.     // Get this year's posts
  20.     $year_posts = get_posts( array(
  21.         'category_name'  => get_query_var( 'category_name' ),
  22.         'year'           => $year,
  23.         'posts_per_page' => -1, // All posts
  24.     ) );
  25.  
  26.     if ( ! $year_posts )
  27.         continue; // No posts for this year
  28.  
  29.     echo "<li>$year<ul>";
  30.  
  31.     foreach ( $year_posts as $year_post ) {
  32.         echo '<li><a href="' . esc_url( get_permalink( $year_post->ID ) ) . '">' . get_the_title( $year_post->ID ) . '</a></li>';
  33.     }
  34.  
  35.     echo '</ul></li>';<ul><li>post 1 from 2011</li><li>post 2 from 2011</li></ul><li>2010</li><ul><li>post 1 from 2010</li><li>post 2 from 2010</li></ul></ul>
  36. }
  37. echo '</ul>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement