single_month_title() WordPress Function

The single_month_title() function is used to display the month and year of a single post. This function can be used inside the WordPress Loop.

single_month_title( string $prefix = '', bool $display = true ) #

Display or retrieve page title for post archive based on date.


Description

Useful for when the template only needs to display the month and year, if either are available. The prefix does not automatically place a space between the prefix, so if there should be a space, the parameter value will need to have it at the end.


Top ↑

Parameters

$prefix

(string)(Optional) What to display before the title.

Default value: ''

$display

(bool)(Optional) Whether to display or retrieve title.

Default value: true


Top ↑

Return

(string|false|void) False if there's no valid title for the month. Title when retrieving.


Top ↑

More Information

  • This tag only works when the m or archive month argument has been passed by WordPress to the current page (this occurs when viewing a monthly archive page).
  • This tag works only on date archive pages, not on category templates or others.
  • It does not support placing the separator after the title, but by leaving the prefix parameter empty, you can set the title separator manually.

Top ↑

Source

File: wp-includes/general-template.php

function single_month_title( $prefix = '', $display = true ) {
	global $wp_locale;

	$m        = get_query_var( 'm' );
	$year     = get_query_var( 'year' );
	$monthnum = get_query_var( 'monthnum' );

	if ( ! empty( $monthnum ) && ! empty( $year ) ) {
		$my_year  = $year;
		$my_month = $wp_locale->get_month( $monthnum );
	} elseif ( ! empty( $m ) ) {
		$my_year  = substr( $m, 0, 4 );
		$my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
	}

	if ( empty( $my_month ) ) {
		return false;
	}

	$result = $prefix . $my_month . $prefix . $my_year;

	if ( ! $display ) {
		return $result;
	}
	echo $result;
}


Top ↑

Changelog

Changelog
VersionDescription
0.71Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More