Using WordPress ‘is_month()’ PHP function

The is_month() WordPress PHP function determines whether the query is for an existing month archive.

Usage

if (is_month()) {
  // Your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: is_month()

Examples

Display a message on month archive pages

if (is_month()) {
  echo 'Welcome to the monthly archive!';
}

Add a special class to the body tag on month archive pages

function add_month_archive_class($classes) {
  if (is_month()) {
    $classes[] = 'month-archive';
  }
  return $classes;
}
add_filter('body_class', 'add_month_archive_class');

Show a specific widget area on month archive pages

if (is_month()) {
  dynamic_sidebar('month-archive-sidebar');
}

Add a custom title to month archive pages

function custom_month_archive_title($title) {
  if (is_month()) {
    $title = 'Monthly Archives: ' . get_the_date('F Y');
  }
  return $title;
}
add_filter('get_the_archive_title', 'custom_month_archive_title');

Display the number of posts in a month archive

if (is_month()) {
  $month_archive = get_queried_object();
  echo 'There are ' . $month_archive->count . ' posts in this month archive.';
}