Viper007Bond

Untitled

Aug 19th, 2011
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 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. // Debug
  18. var_dump( $oldest );
  19. var_dump( $years );
  20.  
  21. // Loop through the years and output the posts for that year
  22. echo '<ul>';
  23. foreach ( $years as $year ) {
  24.  
  25.     // Get this year's posts
  26.     $year_posts = get_posts( array(
  27.         'category_name'  => get_query_var( 'category_name' ),
  28.         'year'           => $year,
  29.         'posts_per_page' => -1, // All posts
  30.     ) );
  31.  
  32.     if ( ! $year_posts )
  33.         continue; // No posts for this year
  34.  
  35.     echo "<li>$year<ul>";
  36.  
  37.     foreach ( $year_posts as $year_post ) {
  38.         echo '<li><a href="' . esc_url( get_permalink( $year_post->ID ) ) . '">' . get_the_title( $year_post->ID ) . '</a></li>';
  39.     }
  40.  
  41.     echo '</ul></li>';
  42.  
  43.     // If we are NOT using an external (persistent) object cache and
  44.     // are instead caching to a variable, then clean this variable
  45.     // out to free up memory.
  46.     if ( ! $_wp_using_ext_object_cache )
  47.         wp_cache_flush();
  48. }
  49. echo '</ul>';
Advertisement
Add Comment
Please, Sign In to add comment