get_calendar

Display calendar with days that have posts as links.

Description

(string|void) get_calendar( (constant) $initial = true, (bool) $echo = true ); 

The calendar is cached, which will be retrieved, if it exists. If there are no posts for the month, then it will not be displayed.

Returns (string|void)

String when retrieving.

Parameters (2)

0. $initial — Optional. (constant) => true
Optional, default is true. Use initial calendar names.
1. $echo — Optional. (bool) => true
Optional, default is true. Set to false for return.

Usage

  1. if ( !function_exists( 'get_calendar' ) ) { 
  2. require_once ABSPATH . WPINC . '/general-template.php'; 
  3.  
  4. // Optional, default is true. Use initial calendar names. 
  5. $initial = true; 
  6.  
  7. // Optional, default is true. Set to false for return. 
  8. $echo = true; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_calendar($initial, $echo); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/general-template.php  
  1. function get_calendar( $initial = true, $echo = true ) { 
  2. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 
  3.  
  4. $key = md5( $m . $monthnum . $year ); 
  5. $cache = wp_cache_get( 'get_calendar', 'calendar' ); 
  6.  
  7. if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) { 
  8. /** This filter is documented in wp-includes/general-template.php */ 
  9. $output = apply_filters( 'get_calendar', $cache[ $key ] ); 
  10.  
  11. if ( $echo ) { 
  12. echo $output; 
  13. return; 
  14.  
  15. return $output; 
  16.  
  17. if ( ! is_array( $cache ) ) { 
  18. $cache = array(); 
  19.  
  20. // Quick check. If we have no posts at all, abort! 
  21. if ( ! $posts ) { 
  22. $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); 
  23. if ( ! $gotsome ) { 
  24. $cache[ $key ] = ''; 
  25. wp_cache_set( 'get_calendar', $cache, 'calendar' ); 
  26. return; 
  27.  
  28. if ( isset( $_GET['w'] ) ) { 
  29. $w = (int) $_GET['w']; 
  30. // week_begins = 0 stands for Sunday 
  31. $week_begins = (int) get_option( 'start_of_week' ); 
  32. $ts = current_time( 'timestamp' ); 
  33.  
  34. // Let's figure out when we are 
  35. if ( ! empty( $monthnum ) && ! empty( $year ) ) { 
  36. $thismonth = zeroise( intval( $monthnum ), 2 ); 
  37. $thisyear = (int) $year; 
  38. } elseif ( ! empty( $w ) ) { 
  39. // We need to get the month from MySQL 
  40. $thisyear = (int) substr( $m, 0, 4 ); 
  41. //it seems MySQL's weeks disagree with PHP's 
  42. $d = ( ( $w - 1 ) * 7 ) + 6; 
  43. $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); 
  44. } elseif ( ! empty( $m ) ) { 
  45. $thisyear = (int) substr( $m, 0, 4 ); 
  46. if ( strlen( $m ) < 6 ) { 
  47. $thismonth = '01'; 
  48. } else { 
  49. $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 ); 
  50. } else { 
  51. $thisyear = gmdate( 'Y', $ts ); 
  52. $thismonth = gmdate( 'm', $ts ); 
  53.  
  54. $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear ); 
  55. $last_day = date( 't', $unixmonth ); 
  56.  
  57. // Get the next and previous month and year with at least one post 
  58. $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 
  59. FROM $wpdb->posts 
  60. WHERE post_date < '$thisyear-$thismonth-01' 
  61. AND post_type = 'post' AND post_status = 'publish' 
  62. ORDER BY post_date DESC 
  63. LIMIT 1"); 
  64. $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 
  65. FROM $wpdb->posts 
  66. WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' 
  67. AND post_type = 'post' AND post_status = 'publish' 
  68. ORDER BY post_date ASC 
  69. LIMIT 1"); 
  70.  
  71. /** translators: Calendar caption: 1: month name, 2: 4-digit year */ 
  72. $calendar_caption = _x('%1$s %2$s', 'calendar caption'); 
  73. $calendar_output = '<table id="wp-calendar"> 
  74. <caption>' . sprintf( 
  75. $calendar_caption,  
  76. $wp_locale->get_month( $thismonth ),  
  77. date( 'Y', $unixmonth ) 
  78. ) . '</caption> 
  79. <thead> 
  80. <tr>'; 
  81.  
  82. $myweek = array(); 
  83.  
  84. for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) { 
  85. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 ); 
  86.  
  87. foreach ( $myweek as $wd ) { 
  88. $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); 
  89. $wd = esc_attr( $wd ); 
  90. $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 
  91.  
  92. $calendar_output .= ' 
  93. </tr> 
  94. </thead> 
  95.  
  96. <tfoot> 
  97. <tr>'; 
  98.  
  99. if ( $previous ) { 
  100. $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">« ' . 
  101. $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) . 
  102. '</a></td>'; 
  103. } else { 
  104. $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; 
  105.  
  106. $calendar_output .= "\n\t\t".'<td class="pad"> </td>'; 
  107.  
  108. if ( $next ) { 
  109. $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link( $next->year, $next->month ) . '">' . 
  110. $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) . 
  111. ' »</a></td>'; 
  112. } else { 
  113. $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; 
  114.  
  115. $calendar_output .= ' 
  116. </tr> 
  117. </tfoot> 
  118.  
  119. <tbody> 
  120. <tr>'; 
  121.  
  122. $daywithpost = array(); 
  123.  
  124. // Get days with posts 
  125. $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) 
  126. FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' 
  127. AND post_type = 'post' AND post_status = 'publish' 
  128. AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); 
  129. if ( $dayswithposts ) { 
  130. foreach ( (array) $dayswithposts as $daywith ) { 
  131. $daywithpost[] = $daywith[0]; 
  132.  
  133. // See how much we should pad in the beginning 
  134. $pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins ); 
  135. if ( 0 != $pad ) { 
  136. $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad"> </td>'; 
  137.  
  138. $newrow = false; 
  139. $daysinmonth = (int) date( 't', $unixmonth ); 
  140.  
  141. for ( $day = 1; $day <= $daysinmonth; ++$day ) { 
  142. if ( isset($newrow) && $newrow ) { 
  143. $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; 
  144. $newrow = false; 
  145.  
  146. if ( $day == gmdate( 'j', $ts ) && 
  147. $thismonth == gmdate( 'm', $ts ) && 
  148. $thisyear == gmdate( 'Y', $ts ) ) { 
  149. $calendar_output .= '<td id="today">'; 
  150. } else { 
  151. $calendar_output .= '<td>'; 
  152.  
  153. if ( in_array( $day, $daywithpost ) ) { 
  154. // any posts today? 
  155. $date_format = date( _x( 'F j, Y', 'daily archives date format' ), strtotime( "{$thisyear}-{$thismonth}-{$day}" ) ); 
  156. /** translators: Post calendar label. 1: Date */ 
  157. $label = sprintf( __( 'Posts published on %s' ), $date_format ); 
  158. $calendar_output .= sprintf( 
  159. '<a href="%s" aria-label="%s">%s</a>',  
  160. get_day_link( $thisyear, $thismonth, $day ),  
  161. esc_attr( $label ),  
  162. $day 
  163. ); 
  164. } else { 
  165. $calendar_output .= $day; 
  166. $calendar_output .= '</td>'; 
  167.  
  168. if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) { 
  169. $newrow = true; 
  170.  
  171. $pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ); 
  172. if ( $pad != 0 && $pad != 7 ) { 
  173. $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr( $pad ) .'"> </td>'; 
  174. $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; 
  175.  
  176. $cache[ $key ] = $calendar_output; 
  177. wp_cache_set( 'get_calendar', $cache, 'calendar' ); 
  178.  
  179. if ( $echo ) { 
  180. /** 
  181. * Filters the HTML calendar output. 
  182. * @since 3.0.0 
  183. * @param string $calendar_output HTML output of the calendar. 
  184. */ 
  185. echo apply_filters( 'get_calendar', $calendar_output ); 
  186. return; 
  187. /** This filter is documented in wp-includes/general-template.php */ 
  188. return apply_filters( 'get_calendar', $calendar_output );