Advertisement
Guest User

Untitled

a guest
Oct 31st, 2012
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. function next_month_link($label = 'Next Month') {
  2. echo get_archive_month_link($label, false);
  3. }
  4.  
  5. function previous_month_link($label = 'Previous Month') {
  6. echo get_archive_month_link($label);
  7. }
  8.  
  9. function get_archive_month_link($label, $previous = true) {
  10.  
  11. $archive_month = get_next_or_previous_month($previous);
  12. if($archive_month) {
  13.  
  14. $year = $archive_month[0]->year;
  15. $month = $archive_month[0]->month;
  16. $full_month = date("F", strtotime("$year-$month"));
  17.  
  18. $label = str_replace(array('%month','%year'), array($full_month,$year), $label);
  19.  
  20. return '<a href="' . get_month_link($year, $month).'">'.$label.'</a>';
  21. }
  22. return '';
  23.  
  24. }
  25.  
  26.  
  27. function get_next_or_previous_month($previous = true) {
  28. $result = '';
  29. if(is_archive() && is_month()) {
  30. $year = get_query_var('year');
  31. $month = get_query_var('monthnum');
  32. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  33.  
  34. if($previous) {
  35. $first_day = $year. '-'. $month . '-01 00:00:00';
  36. $post_date = " AND post_date < '$first_day'";
  37. $order = "DESC";
  38.  
  39. } else {
  40. $last_day = date("Y-m-d H:i:s", strtotime("first day of next month" . $year. '-'.$month));
  41. $post_date = " AND post_date >= '$last_day'";
  42. $order = "ASC";
  43. }
  44. global $wpdb;
  45. $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish'$post_date GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date $order LIMIT 0, 1";
  46. $result = $wpdb->get_results($query);
  47. }
  48. return $result;
  49. }
  50.  
  51.  
  52. add_action( 'pre_get_posts', 'monthly_pagination' );
  53. function monthly_pagination( $query ) {
  54. if (!is_admin() && $query->is_main_query()){
  55. if (is_archive() && $query->is_month() ) {
  56. $query->set( 'nopaging', true );
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement