Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function next_month_link($label = 'Next Month') {
- echo get_archive_month_link($label, false);
- }
- function previous_month_link($label = 'Previous Month') {
- echo get_archive_month_link($label);
- }
- function get_archive_month_link($label, $previous = true) {
- $archive_month = get_next_or_previous_month($previous);
- if($archive_month) {
- $year = $archive_month[0]->year;
- $month = $archive_month[0]->month;
- $full_month = date("F", strtotime("$year-$month"));
- $label = str_replace(array('%month','%year'), array($full_month,$year), $label);
- return '<a href="' . get_month_link($year, $month).'">'.$label.'</a>';
- }
- return '';
- }
- function get_next_or_previous_month($previous = true) {
- $result = '';
- if(is_archive() && is_month()) {
- $year = get_query_var('year');
- $month = get_query_var('monthnum');
- $month = str_pad($month, 2, "0", STR_PAD_LEFT);
- if($previous) {
- $first_day = $year. '-'. $month . '-01 00:00:00';
- $post_date = " AND post_date < '$first_day'";
- $order = "DESC";
- } else {
- $last_day = date("Y-m-d H:i:s", strtotime("first day of next month" . $year. '-'.$month));
- $post_date = " AND post_date >= '$last_day'";
- $order = "ASC";
- }
- global $wpdb;
- $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";
- $result = $wpdb->get_results($query);
- }
- return $result;
- }
- add_action( 'pre_get_posts', 'monthly_pagination' );
- function monthly_pagination( $query ) {
- if (!is_admin() && $query->is_main_query()){
- if (is_archive() && $query->is_month() ) {
- $query->set( 'nopaging', true );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement