• Hello all,

    I’m interested in displaying a calendar of my posts using get_calendar. But I’d like to display the previous month — NOT the current month. I haven’t found a way to do this, nor have I found a plugin that can do it, either.

    Any suggestions?

    Thank you for your time,
    Osgood

Viewing 11 replies - 1 through 11 (of 11 total)
  • http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/general-template.php
    make a copy of the get_calendar function and name it something else and put it in your active theme’s function.php file. Modify the function to get the previous month’s posts: subtract 1 from $monthnum, if $monthnum = 0 then set it to 12 and subtract 1 from year

    1074	function get_calendar($initial = true, $echo = true) {
    1075	        global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    1076
    1077	        $cache = array();
    1078	        $key = md5( $m . $monthnum . $year );
    1079	        if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
    1080	                if ( is_array($cache) && isset( $cache[ $key ] ) ) {
    1081	                        if ( $echo ) {
    1082	                                echo apply_filters( 'get_calendar',  $cache[$key] );
    1083	                                return;
    1084	                        } else {
    1085	                                return apply_filters( 'get_calendar',  $cache[$key] );
    1086	                        }
    1087	                }
    1088	        }
    1089
    1090	        if ( !is_array($cache) )
    1091	                $cache = array();
    1092
    1093	        // Quick check. If we have no posts at all, abort!
    1094	        if ( !$posts ) {
    1095	                $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
    1096	                if ( !$gotsome ) {
    1097	                        $cache[ $key ] = '';
    1098	                        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    1099	                        return;
    1100	                }
    1101	        }
    1102
    1103	        if ( isset($_GET['w']) )
    1104	                $w = ''.intval($_GET['w']);
    1105
    1106	        // week_begins = 0 stands for Sunday
    1107	        $week_begins = intval(get_option('start_of_week'));
    1108
    1109	        // Let's figure out when we are
    1110	        if ( !empty($monthnum) && !empty($year) ) {
    1111	                $thismonth = ''.zeroise(intval($monthnum), 2);
    1112	                $thisyear = ''.intval($year);
    1113	        } elseif ( !empty($w) ) {
    1114	                // We need to get the month from MySQL
    1115	                $thisyear = ''.intval(substr($m, 0, 4));
    1116	                $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
    1117	                $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
    1118	        } elseif ( !empty($m) ) {
    1119	                $thisyear = ''.intval(substr($m, 0, 4));
    1120	                if ( strlen($m) < 6 )
    1121	                                $thismonth = '01';
    1122	                else
    1123	                                $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
    1124	        } else {
    1125	                $thisyear = gmdate('Y', current_time('timestamp'));
    1126	                $thismonth = gmdate('m', current_time('timestamp'));
    1127	        }
    1128
    1129	        $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
    1130	        $last_day = date('t', $unixmonth);
    1131
    1132	        // Get the next and previous month and year with at least one post
    1133	        $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    1134	                FROM $wpdb->posts
    1135	                WHERE post_date < '$thisyear-$thismonth-01'
    1136	                AND post_type = 'post' AND post_status = 'publish'
    1137	                        ORDER BY post_date DESC
    1138	                        LIMIT 1");
    1139	        $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    1140	                FROM $wpdb->posts
    1141	                WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    1142	                AND post_type = 'post' AND post_status = 'publish'
    1143	                        ORDER BY post_date ASC
    1144	                        LIMIT 1");
    1145
    1146	        /* translators: Calendar caption: 1: month name, 2: 4-digit year */
    1147	        $calendar_caption = _x('%1$s %2$s', 'calendar caption');
    1148	        $calendar_output = '<table id="wp-calendar">
    1149	        <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
    1150	        <thead>
    1151	        <tr>';
    1152
    1153	        $myweek = array();
    1154
    1155	        for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
    1156	                $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
    1157	        }
    1158
    1159	        foreach ( $myweek as $wd ) {
    1160	                $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
    1161	                $wd = esc_attr($wd);
    1162	                $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    1163	        }
    1164
    1165	        $calendar_output .= '
    1166	        </tr>
    1167	        </thead>
    1168
    1169	        <tfoot>
    1170	        <tr>';
    1171
    1172	        if ( $previous ) {
    1173	                $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
    1174	        } else {
    1175	                $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
    1176	        }
    1177
    1178	        $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
    1179
    1180	        if ( $next ) {
    1181	                $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
    1182	        } else {
    1183	                $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
    1184	        }
    1185
    1186	        $calendar_output .= '
    1187	        </tr>
    1188	        </tfoot>
    1189
    1190	        <tbody>
    1191	        <tr>';
    1192
    1193	        // Get days with posts
    1194	        $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    1195	                FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    1196	                AND post_type = 'post' AND post_status = 'publish'
    1197	                AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
    1198	        if ( $dayswithposts ) {
    1199	                foreach ( (array) $dayswithposts as $daywith ) {
    1200	                        $daywithpost[] = $daywith[0];
    1201	                }
    1202	        } else {
    1203	                $daywithpost = array();
    1204	        }
    1205
    1206	        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
    1207	                $ak_title_separator = "\n";
    1208	        else
    1209	                $ak_title_separator = ', ';
    1210
    1211	        $ak_titles_for_day = array();
    1212	        $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    1213	                ."FROM $wpdb->posts "
    1214	                ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    1215	                ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    1216	                ."AND post_type = 'post' AND post_status = 'publish'"
    1217	        );
    1218	        if ( $ak_post_titles ) {
    1219	                foreach ( (array) $ak_post_titles as $ak_post_title ) {
    1220
    1221	                                $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
    1222
    1223	                                if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
    1224	                                        $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
    1225	                                if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
    1226	                                        $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
    1227	                                else
    1228	                                        $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
    1229	                }
    1230	        }
    1231
    1232
    1233	        // See how much we should pad in the beginning
    1234	        $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
    1235	        if ( 0 != $pad )
    1236	                $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
    1237
    1238	        $daysinmonth = intval(date('t', $unixmonth));
    1239	        for ( $day = 1; $day <= $daysinmonth; ++$day ) {
    1240	                if ( isset($newrow) && $newrow )
    1241	                        $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    1242	                $newrow = false;
    1243
    1244	                if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
    1245	                        $calendar_output .= '<td id="today">';
    1246	                else
    1247	                        $calendar_output .= '<td>';
    1248
    1249	                if ( in_array($day, $daywithpost) ) // any posts today?
    1250	                                $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
    1251	                else
    1252	                        $calendar_output .= $day;
    1253	                $calendar_output .= '</td>';
    1254
    1255	                if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
    1256	                        $newrow = true;
    1257	        }
    1258
    1259	        $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
    1260	        if ( $pad != 0 && $pad != 7 )
    1261	                $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
    1262
    1263	        $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
    1264
    1265	        $cache[ $key ] = $calendar_output;
    1266	        wp_cache_set( 'get_calendar', $cache, 'calendar' );
    1267
    1268	        if ( $echo )
    1269	                echo apply_filters( 'get_calendar',  $calendar_output );
    1270	        else
    1271	                return apply_filters( 'get_calendar',  $calendar_output );
    1272
    1273	}

    Thread Starter deadmentor

    (@deadmentor)

    Thanks for responding! That makes a lot of sense, but — and I apologize for this — I just don’t know how to do what you suggested. I copied and pasted the code into my functions.php file and renamed the get_calendar function no problem — but that’s as far as I got.

    Do you think you could break the rest of it down into steps for me?

    Thank you very much for your time and patience!

    Best,
    Osgood

    PS. Also, will this update every month so that it always displays the previous month? That’s important.

    This will update every month so the the calendar is always of the previous month

    step 1 make copy of get_calendar function

    step 2 add a filter and a callback function (ucc_get_calendar_filter)

    the filter says to wordpress when get_calendar function is called do ucc_get_calendar instead

    here’s code that makes it previous month instead of current one

    $thismonth = ''.zeroise(intval($thismonth - 1), 2);
    	if ($thismonth == '0'){
    		$thismonth = 12;
    		$thisyear = intval($thisyear - 1);
    	}

    and everything together:

    function ucc_get_calendar($initial = true, $echo = true) {
    	global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    	$cache = array();
    	$key = md5( $m . $monthnum . $year );
    /*	if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
    		if ( is_array($cache) && isset( $cache[ $key ] ) ) {
    			if ( $echo ) {
    				echo apply_filters( 'get_calendar',  $cache[$key] );
    				return;
    			} else {
    				return apply_filters( 'get_calendar',  $cache[$key] );
    			}
    		}
    	}*/
    
    	if ( !is_array($cache) )
    		$cache = array();
    
    	// Quick check. If we have no posts at all, abort!
    	if ( !$posts ) {
    		$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
    		if ( !$gotsome ) {
    			$cache[ $key ] = '';
    			wp_cache_set( 'get_calendar', $cache, 'calendar' );
    			return;
    		}
    	}
    
    	if ( isset($_GET['w']) )
    		$w = ''.intval($_GET['w']);
    
    	// week_begins = 0 stands for Sunday
    	$week_begins = intval(get_option('start_of_week'));
    
    	// Let's figure out when we are
    	if ( !empty($monthnum) && !empty($year) ) {
    		$thismonth = ''.zeroise(intval($monthnum), 2);
    		$thisyear = ''.intval($year);
    	} elseif ( !empty($w) ) {
    		// We need to get the month from MySQL
    		$thisyear = ''.intval(substr($m, 0, 4));
    		$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
    		$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
    	} elseif ( !empty($m) ) {
    		$thisyear = ''.intval(substr($m, 0, 4));
    		if ( strlen($m) < 6 )
    				$thismonth = '01';
    		else
    				$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
    	} else {
    		$thisyear = gmdate('Y', current_time('timestamp'));
    		$thismonth = gmdate('m', current_time('timestamp'));
    	}
    	$thismonth = ''.zeroise(intval($thismonth - 1), 2);
    	if ($thismonth == '0'){
    		$thismonth = 12;
    		$thisyear = intval($thisyear - 1);
    	}
    //	die('fdgfddgfdgfdgf         '.$thismonth);
    	$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
    	$last_day = date('t', $unixmonth);
    
    	// Get the next and previous month and year with at least one post
    	$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date < '$thisyear-$thismonth-01'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date DESC
    			LIMIT 1");
    	$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date ASC
    			LIMIT 1");
    
    	/* translators: Calendar caption: 1: month name, 2: 4-digit year */
    	$calendar_caption = _x('%1$s %2$s', 'calendar caption');
    	$calendar_output = '<table id="wp-calendar">
    	<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
    	<thead>
    	<tr>';
    
    	$myweek = array();
    
    	for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
    		$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
    	}
    
    	foreach ( $myweek as $wd ) {
    		$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
    		$wd = esc_attr($wd);
    		$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    	}
    
    	$calendar_output .= '
    	</tr>
    	</thead>
    
    	<tfoot>
    	<tr>';
    
    	if ( $previous ) {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
    	} else {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
    	}
    
    	$calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
    
    	if ( $next ) {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
    	} else {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
    	}
    
    	$calendar_output .= '
    	</tr>
    	</tfoot>
    
    	<tbody>
    	<tr>';
    
    	// Get days with posts
    	$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    		FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    		AND post_type = 'post' AND post_status = 'publish'
    		AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
    	if ( $dayswithposts ) {
    		foreach ( (array) $dayswithposts as $daywith ) {
    			$daywithpost[] = $daywith[0];
    		}
    	} else {
    		$daywithpost = array();
    	}
    
    	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
    		$ak_title_separator = "\n";
    	else
    		$ak_title_separator = ', ';
    
    	$ak_titles_for_day = array();
    	$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    		."FROM $wpdb->posts "
    		."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    		."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    		."AND post_type = 'post' AND post_status = 'publish'"
    	);
    	if ( $ak_post_titles ) {
    		foreach ( (array) $ak_post_titles as $ak_post_title ) {
    
    				$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
    
    				if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
    					$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
    				if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
    					$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
    				else
    					$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
    		}
    	}
    
    	// See how much we should pad in the beginning
    	$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
    	if ( 0 != $pad )
    		$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
    
    	$daysinmonth = intval(date('t', $unixmonth));
    	for ( $day = 1; $day <= $daysinmonth; ++$day ) {
    		if ( isset($newrow) && $newrow )
    			$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    		$newrow = false;
    
    		if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
    			$calendar_output .= '<td id="today">';
    		else
    			$calendar_output .= '<td>';
    
    		if ( in_array($day, $daywithpost) ) // any posts today?
    				$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
    		else
    			$calendar_output .= $day;
    		$calendar_output .= '</td>';
    
    		if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
    			$newrow = true;
    	}
    
    	$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
    	if ( $pad != 0 && $pad != 7 )
    		$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
    
    	$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
    
    	$cache[ $key ] = $calendar_output;
    	wp_cache_set( 'get_calendar', $cache, 'calendar' );
    
    	if ( $echo )
    		echo $calendar_output;
    	else
    		return $calendar_output;
    
    }
    
    function ucc_get_calendar_filter( $content ) {
      $output = ucc_get_calendar();
      return $output;
    }
    add_filter( 'get_calendar' , 'ucc_get_calendar_filter' , 10 , 2 );
    Thread Starter deadmentor

    (@deadmentor)

    That works exactly! Thank you for your help.

    Unfortunately, I’ve realized that this won’t accomplish my utmost goal, which is to display two months worth of posts in calendar form, the current month AND the previous month. I thought knowing how to display the previous month would solve my problem, but of course it doesn’t.

    I’d be grateful for any help or insight you have into accomplishing this — but you’ve already done so much, and I understand if you’d like to move on.

    Regardless, thank you very much for your time and assistance. I learned a lot.

    Best,
    Osgood

    decide if you need the next and prev links to still work,
    by default i understand you want this months and previous months posts in calendar form, but do you want previous button to be below them so they can see previous two months if they click it, and then go forwards or keep going back until no more posts?

    Thread Starter deadmentor

    (@deadmentor)

    Yes, ideally I’d like the previous and next links to work in the way you describe, that is showing the previous/next two months. But having those links isn’t THAT important, if it’s too complicated or time-intensive for you.

    Again, thank you so much for your help. You’re making my day!

    Best,
    Osgood

    nvm that doesnt work šŸ˜€

    Thread Starter deadmentor

    (@deadmentor)

    Really, the most important thing is displaying the two months at once, current and previous. That’s the key. Everything else is dessert!

    function ucc_get_calendar($initial = true, $echo = true) {
    	global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
    	$cache = array();
    	$key = md5( $m . $monthnum . $year );
    /*	if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
    		if ( is_array($cache) && isset( $cache[ $key ] ) ) {
    			if ( $echo ) {
    				echo apply_filters( 'get_calendar',  $cache[$key] );
    				return;
    			} else {
    				return apply_filters( 'get_calendar',  $cache[$key] );
    			}
    		}
    	}*/
    
    	if ( !is_array($cache) )
    		$cache = array();
    
    	// Quick check. If we have no posts at all, abort!
    	if ( !$posts ) {
    		$gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
    		if ( !$gotsome ) {
    			$cache[ $key ] = '';
    			wp_cache_set( 'get_calendar', $cache, 'calendar' );
    			return;
    		}
    	}
    
    	if ( isset($_GET['w']) )
    		$w = ''.intval($_GET['w']);
    
    	// week_begins = 0 stands for Sunday
    	$week_begins = intval(get_option('start_of_week'));
    
    	// Let's figure out when we are
    	if ( !empty($monthnum) && !empty($year) ) {
    		$thismonth = ''.zeroise(intval($monthnum), 2);
    		$thisyear = ''.intval($year);
    	} elseif ( !empty($w) ) {
    		// We need to get the month from MySQL
    		$thisyear = ''.intval(substr($m, 0, 4));
    		$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
    		$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
    	} elseif ( !empty($m) ) {
    		$thisyear = ''.intval(substr($m, 0, 4));
    		if ( strlen($m) < 6 )
    				$thismonth = '01';
    		else
    				$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
    	} else {
    		$thisyear = gmdate('Y', current_time('timestamp'));
    		$thismonth = gmdate('m', current_time('timestamp'));
    	}
    	$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
    	$last_day = date('t', $unixmonth);
    
    	// Get the next and previous month and year with at least one post
    	$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date < '$thisyear-$thismonth-01'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date DESC
    			LIMIT 1");
    	$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date ASC
    			LIMIT 1");
    
    	/* translators: Calendar caption: 1: month name, 2: 4-digit year */
    	$calendar_caption = _x('%1$s %2$s', 'calendar caption');
    	$calendar_output = '<table id="wp-calendar">
    	<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
    	<thead>
    	<tr>';
    
    	$myweek = array();
    
    	for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
    		$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
    	}
    
    	foreach ( $myweek as $wd ) {
    		$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
    		$wd = esc_attr($wd);
    		$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    	}
    
    	$calendar_output .= '
    	</tr>
    	</thead>
    
    	<tbody>
    	<tr>';
    
    	// Get days with posts
    	$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    		FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    		AND post_type = 'post' AND post_status = 'publish'
    		AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
    	if ( $dayswithposts ) {
    		foreach ( (array) $dayswithposts as $daywith ) {
    			$daywithpost[] = $daywith[0];
    		}
    	} else {
    		$daywithpost = array();
    	}
    
    	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
    		$ak_title_separator = "\n";
    	else
    		$ak_title_separator = ', ';
    
    	$ak_titles_for_day = array();
    	$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    		."FROM $wpdb->posts "
    		."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    		."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    		."AND post_type = 'post' AND post_status = 'publish'"
    	);
    	if ( $ak_post_titles ) {
    		foreach ( (array) $ak_post_titles as $ak_post_title ) {
    
    				$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
    
    				if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
    					$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
    				if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
    					$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
    				else
    					$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
    		}
    	}
    
    	// See how much we should pad in the beginning
    	$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
    	if ( 0 != $pad )
    		$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
    
    	$daysinmonth = intval(date('t', $unixmonth));
    	for ( $day = 1; $day <= $daysinmonth; ++$day ) {
    		if ( isset($newrow) && $newrow )
    			$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    		$newrow = false;
    
    		if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
    			$calendar_output .= '<td id="today">';
    		else
    			$calendar_output .= '<td>';
    
    		if ( in_array($day, $daywithpost) ) // any posts today?
    				$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
    		else
    			$calendar_output .= $day;
    		$calendar_output .= '</td>';
    
    		if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
    			$newrow = true;
    	}
    
    	$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
    	if ( $pad != 0 && $pad != 7 )
    		$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
    
    	$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
    
    	$thismonth = ''.zeroise(intval($thismonth - 1), 2);
    	if ($thismonth == '0'){
    		$thismonth = 12;
    		$thisyear = intval($thisyear - 1);
    	}
    //	die('fdgfddgfdgfdgf         '.$thismonth);
    	$thatmonth = $thismonth - 1;
    	$thatyear = $thisyear;
    	if ($thatmonth == '0'){
    		$thatmonth = 12;
    		$thatyear = intval($thisyear - 1);
    	}
    	$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
    	$last_day = date('t', $unixmonth);
    	// Get the next and previous month and year with at least one post
    	$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date < '$thisyear-$thismonth-01'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date DESC
    			LIMIT 1");
    	$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    		FROM $wpdb->posts
    		WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    		AND post_type = 'post' AND post_status = 'publish'
    			ORDER BY post_date ASC
    			LIMIT 1");
    
    	/* translators: Calendar caption: 1: month name, 2: 4-digit year */
    	$calendar_caption = _x('%1$s %2$s', 'calendar caption');
    	$calendar_output .= '<table id="wp-calendar">
    	<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
    	<thead>
    	<tr>';
    
    	$myweek = array();
    
    	for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
    		$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
    	}
    
    	foreach ( $myweek as $wd ) {
    		$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
    		$wd = esc_attr($wd);
    		$calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
    	}
    
    	$calendar_output .= '
    	</tr>
    	</thead>
    
    	<tfoot>
    	<tr>';
    
    	if ( $previous ) {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>';
    	} else {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
    	}
    
    	$calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
    
    	if ( $next ) {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' &raquo;</a></td>';
    	} else {
    		$calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
    	}
    
    	$calendar_output .= '
    	</tr>
    	</tfoot>
    
    	<tbody>
    	<tr>';
    
    	// Get days with posts
    	$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
    		FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    		AND post_type = 'post' AND post_status = 'publish'
    		AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N);
    	if ( $dayswithposts ) {
    		foreach ( (array) $dayswithposts as $daywith ) {
    			$daywithpost[] = $daywith[0];
    		}
    	} else {
    		$daywithpost = array();
    	}
    
    	if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
    		$ak_title_separator = "\n";
    	else
    		$ak_title_separator = ', ';
    
    	$ak_titles_for_day = array();
    	$ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
    		."FROM $wpdb->posts "
    		."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
    		."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
    		."AND post_type = 'post' AND post_status = 'publish'"
    	);
    	if ( $ak_post_titles ) {
    		foreach ( (array) $ak_post_titles as $ak_post_title ) {
    
    				$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
    
    				if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
    					$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
    				if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
    					$ak_titles_for_day["$ak_post_title->dom"] = $post_title;
    				else
    					$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
    		}
    	}
    
    	// See how much we should pad in the beginning
    	$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
    	if ( 0 != $pad )
    		$calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
    
    	$daysinmonth = intval(date('t', $unixmonth));
    	for ( $day = 1; $day <= $daysinmonth; ++$day ) {
    		if ( isset($newrow) && $newrow )
    			$calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
    		$newrow = false;
    
    		if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
    			$calendar_output .= '<td id="today">';
    		else
    			$calendar_output .= '<td>';
    
    		if ( in_array($day, $daywithpost) ) // any posts today?
    				$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
    		else
    			$calendar_output .= $day;
    		$calendar_output .= '</td>';
    
    		if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
    			$newrow = true;
    	}
    
    	$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
    	if ( $pad != 0 && $pad != 7 )
    		$calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
    
    	$calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
    
    	$cache[ $key ] = $calendar_output;
    	wp_cache_set( 'get_calendar', $cache, 'calendar' );
    
    	if ( $echo )
    		echo $calendar_output;
    	else
    		return $calendar_output;
    
    }
    
    function ucc_get_calendar_filter( $content ) {
      $output = ucc_get_calendar();
      return $output;
    }
    add_filter( 'get_calendar' , 'ucc_get_calendar_filter' , 10 , 2 );
    Thread Starter deadmentor

    (@deadmentor)

    That is fantastic and exactly, exactly what I wanted! Wow. Pat yourself on the back. That’s the best.

    Thanks once more for all your time and help. Utterly, utterly great!

    Best,
    Osgood

    Thread Starter deadmentor

    (@deadmentor)

    Unfortunately, it looks like this doesn’t quite accomplish what I’d like.

    The second month displays posts from both months — in other words, if December and November are the months displayed, the December calendar only shows December posts but the November calendar shows both November AND December.

    It’s so close, though! I tried to do what I could, but I couldn’t get very far. Any suggestions?

    Best,
    Osgood

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Using get_calendar to display previous months?’ is closed to new replies.