Monthly archive divided by years


/ Published in: PHP
Save to your folder(s)

created by Adi Dragus


Copy this code and paste it in your HTML
  1. /*-----------------------------------------------------------------------------------------------*/
  2. /* GET MONTHLY ARCHIVE DIVIDED TO YEARS */
  3. /*-----------------------------------------------------------------------------------------------*/
  4. /*
  5.  * splits:
  6.  * <h3>$Year</h3>
  7.  * <ul>
  8.  * <li><a>$MonthName</a> $Number posts</li>
  9.  * </ul>
  10.  */
  11.  
  12. function get_by_year($sql, $args) {
  13.  
  14. global $wpdb;
  15.  
  16. return $sql .= $wpdb->prepare(" AND YEAR(`post_date`) = '%s'", $args['year']);
  17.  
  18. }
  19.  
  20. function get_archive_by_year() {
  21.  
  22. $archiveString = wp_get_archives('type=yearly&echo=0');
  23.  
  24. preg_match_all("#title='(\d{4})'#", $archiveString, $matches);
  25.  
  26. foreach ($matches[1] as $year):
  27.  
  28. echo "<h3>" . $year . "</h3>";
  29.  
  30. add_filter('getarchives_where', "get_by_year" , 666, 2);
  31.  
  32. $monthlyArchives = preg_replace('#(.+)(\s\d{4})(</.+>)#', "$1 $3", wp_get_archives("type=monthly&echo=0&show_post_count=1&year=$year"));
  33.  
  34. $monthlyArchives = str_replace( '(', '', $monthlyArchives );
  35.  
  36. $monthlyArchives = str_replace( ')', ' posts', $monthlyArchives );
  37.  
  38. echo "<ul>" . $monthlyArchives . "</ul>";
  39.  
  40. remove_filter('getarchives_where', 666);
  41.  
  42. endforeach;
  43.  
  44. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.