the_title() WordPress Function

The the_title() function is used to display the title of a post or page. It can be used within the loop or outside the loop.

the_title( string $before = '', string $after = '', bool $echo = true ) #

Displays or retrieves the current post title with optional markup.


Parameters

$before

(string)(Optional) Markup to prepend to the title.

Default value: ''

$after

(string)(Optional) Markup to append to the title.

Default value: ''

$echo

(bool)(Optional) Whether to echo or return the title. Default true for echo.

Default value: true


Top ↑

Return

(void|string) Void if $echo argument is true, current post title if $echo is false.


Top ↑

More Information

This function displays or returns the unescaped title of the current post. This tag may only be used within The Loop, to get the title of a post outside of the loop use get_the_title. If the post is protected or private, this will be noted by the words “Protected: ” or “Private: ” prepended to the title.

Security considerations

Like the_content() , the output of the_title() is unescaped. This is considered a feature and not a bug, see the FAQ “Why are some users allowed to post unfiltered HTML?” . If the post title is <script>alert("test");</script>, then that JavaScript code will be run wherever the_title() is used. For this reason, do not write code that allows untrusted users to create post titles.


Top ↑

Source

File: wp-includes/post-template.php

function the_title( $before = '', $after = '', $echo = true ) {
	$title = get_the_title();

	if ( strlen( $title ) == 0 ) {
		return;
	}

	$title = $before . $title . $after;

	if ( $echo ) {
		echo $title;
	} else {
		return $title;
	}
}


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.