WordPress at a glance

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.

Is the basis for: wp_get_document_title()
✈ 1 time = 0.00001s = speed of light | 50000 times = 0.3093s = very fast
Hooks from 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

#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>

Code of single_post_title: wp-includes/general-template.php VER 4.9.8

<?php
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 object $_post       The current queried object as returned by get_queried_object().
	 */
	$title = apply_filters( 'single_post_title', $_post->post_title, $_post );
	if ( $display )
		echo $prefix . $title;
	else
		return $prefix . $title;
}

Related Functions

From tag: title (of post page)

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register