the_title() WP 0.71
Display the current post title with optional HTML markup. It is accepted to use inside the WordPress Loop.
If the post is protected or private and you are on front-end, then "Protected" or "Private" will be displayed before the post title.
When you need to add post title to an HTML tag attribute, use the_title_attribute() function instead of the_title() or get_the_title().
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
Works based on: get_the_title()
1 time — 0.004764 sec (very slow) | 50000 times — 1.99 sec (fast) | PHP 7.1.2, WP 4.7.3
No Hooks.
Return
String/null. null when the result is displayed ($echo=true). String when ($echo=false).
Usage
the_title( $before, $after, $echo );
- $before(string)
- Text / HTML code to be placed before the title.
Default: '' - $after(string)
- Text / HTML code to be placed after the title.
Default: '' - $echo(true/false)
Whether to echo or return the title.
- true — display
- false — return for further processing.
Default: true
Examples
#1 Display the title of the post in the html tag H3
<?php the_title('<h3>', '</h3>'); ?>
or
<h3><?php the_title(); ?></h3>
#2 Get the title of the post in the variable
To get the value to use it in PHP, specify the third parameter $echo as false or 0
. Or use the get_the_title()
$title = the_title( '', '', 0 ); // or $title = get_the_title();
Changelog
Since 0.71 | Introduced. |
Code of the_title() the title WP 5.6.2
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;
}
}