The get_calendar() WordPress PHP function displays a calendar with days that have posts as links.
Usage
get_calendar($initial = true, $display = true);
Parameters
- $initial (bool) – Optional. Whether to use initial calendar names. Default: true.
- $display (bool) – Optional. Whether to display the calendar output. Default: true.
More information
See WordPress Developer Resources: get_calendar()
Examples
Display a calendar with full month names
Display a calendar with full month names instead of initials.
get_calendar(false);
Store calendar output in a variable
Store the calendar output in a variable $calendar
instead of directly displaying it.
$calendar = get_calendar(true, false);
Display calendar with a custom CSS class
Add a custom CSS class to the calendar by wrapping the get_calendar() function in a div
element.
<div class="custom-calendar"> <?php get_calendar(); ?> </div>
Display multiple calendars for different months
Display multiple calendars by using the get_calendar() function inside a loop and filtering posts by month.
for ($month = 1; $month <= 12; $month++) { query_posts("monthnum=$month&year=2023"); get_calendar(); wp_reset_query(); }
Display calendar in a custom widget
Create a custom widget to display the calendar using the get_calendar() function.
class Custom_Calendar_Widget extends WP_Widget { public function __construct() { parent::__construct( 'custom_calendar_widget', 'Custom Calendar', array('description' => 'A custom calendar widget') ); } public function widget($args, $instance) { echo $args['before_widget']; echo $args['before_title'] . 'Calendar' . $args['after_title']; get_calendar(); echo $args['after_widget']; } } function register_custom_calendar_widget() { register_widget('Custom_Calendar_Widget'); } add_action('widgets_init', 'register_custom_calendar_widget');