single_post_title()WP 0.71

Displays or retrieve page title for post. Designed for use on individual post pages.

This is optimized for single.php template file for displaying the post title.

This function (template tag) is useful when you want to display the title of a post outside of WordPress Loop.

It does not support placing the separator after the title, but by leaving the prefix parameter empty, you can set the title separator manually. 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.

1 time — 0.00001 sec (speed of light) | 50000 times — 0.3093 sec (very fast)
Hooks from the function

Return

String|null. Title when retrieving.

Usage

single_post_title( $prefix, $display );
$prefix(string)
What to display before the title.
Default: ''
$display(true|false)
Whether to display or retrieve title.
Default: true

Examples

0

#1 The output of the post title

Output the title of the entry in the H2 tag with the prefix text "Current post: ":

<h2><?php single_post_title('Current post: '); ?></h2>

Changelog

Since 0.71 Introduced.

single_post_title() code WP 6.4.3

function single_post_title( $prefix = '', $display = true ) {
	$_post = get_queried_object();

	if ( ! isset( $_post->post_title ) ) {
		return;
	}

	/**
	 * Filters the page title for a single post.
	 *
	 * @since 0.71
	 *
	 * @param string  $_post_title The single post page title.
	 * @param WP_Post $_post       The current post.
	 */
	$title = apply_filters( 'single_post_title', $_post->post_title, $_post );
	if ( $display ) {
		echo $prefix . $title;
	} else {
		return $prefix . $title;
	}
}