single_month_title()WP 0.71

Outputs or retrieves the title of the monthly archive page of posts.

Output format: prefix + MONTH + prefix + YEAR.

This Template Tag works only on monthly archive pages (where the argument m is passed in the query). On category, tag, author, etc. output pages, the function will not output anything.

This tag is primarily used to display the page title, for example, in the <title> tag.

No Hooks.

Returns

String|false|null.

Usage

<?php single_month_title( $prefix, $display ) ?>
$prefix(string)
Text to place before the title.
Default: ''
$display(boolean)
Output to screen (true) or return for processing (false).
Default: true

Examples

0

#1 A basic example.

Let's print a header for the page:

<?php single_month_title( ) ?>

The output format will be:

prefix + MONTH + prefix + YEAR

If we specify the prefix *, we get:

*February*2004
0

#2 You can use html tags in the prefix:

<p><?php single_month_title('<br />') ?></p>

Output is following 2 lines.

December
2004

Notes

  • Global. WP_Locale. $wp_locale WordPress date and time locale object.

Changelog

Since 0.71 Introduced.

single_month_title() code WP 7.0

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;
}