Viper007Bond

Untitled

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