WordPress at a glance

get_the_title() WP 0.71

Gets the title of the post for further use in php. You can get the title of the specified post.

The function can be used inside a WordPress Loop without specifying a parameter, then the title of the current post will be returned. Or pass a post ID in the first parameter and the function will return the title of this specified post.

If the post is protected or private and you are on front-end, then "Protected" or "Private" will be displayed before the post title.

There is no function get_post_title() in WordPress, which logically should exist. Use this function instead.

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>

Value of get_the_title() recommended to be cleaned before displaying because it displays all HTML tags as is, and it is not safe. There are two ways to clean it:

  • Full cleaning. Prevents the use of any HTML tags:

    echo esc_html( get_the_title() );
  • Partial cleaning. Allows you to leave the basic HTML tags (performance in this case suffers):
    echo wp_kses_post( get_the_title() );
Is the basis for: the_title()
✈ 1 time = 0.000346s = fast | 50000 times = 0.68s = very fast PHP 7.1.0, WP 4.7
Hooks from function:
Return

String. Post title.

Usage

get_the_title( $post );
$post(int/WP_Post)
Post ID or WP_Post object.
Default: global $post

Examples

#1 Print the current post title

echo get_the_title();

// or in H1 tag
echo '<h1>'. esc_html( get_the_title() ) .'</h1>';

#2 Print the title of post 25

echo get_the_title( 25 );

// or pass parameter as object
$the_post = get_post( 25 );
echo get_the_title( $the_post );

Code of get_the_title: wp-includes/post-template.php VER 4.9.8

<?php
function get_the_title( $post = 0 ) {
	$post = get_post( $post );

	$title = isset( $post->post_title ) ? $post->post_title : '';
	$id = isset( $post->ID ) ? $post->ID : 0;

	if ( ! is_admin() ) {
		if ( ! empty( $post->post_password ) ) {

			/**
			 * Filters the text prepended to the post title for protected posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Protected: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ), $post );
			$title = sprintf( $protected_title_format, $title );
		} elseif ( isset( $post->post_status ) && 'private' == $post->post_status ) {

			/**
			 * Filters the text prepended to the post title of private posts.
			 *
			 * The filter is only applied on the front end.
			 *
			 * @since 2.8.0
			 *
			 * @param string  $prepend Text displayed before the post title.
			 *                         Default 'Private: %s'.
			 * @param WP_Post $post    Current post object.
			 */
			$private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ), $post );
			$title = sprintf( $private_title_format, $title );
		}
	}

	/**
	 * Filters the post title.
	 *
	 * @since 0.71
	 *
	 * @param string $title The post title.
	 * @param int    $id    The post ID.
	 */
	return apply_filters( 'the_title', $title, $id );
}

Related Functions

From tag: title (of post page)

More from category: Posts, pages...

No comments
    Hello, !     Log In . Register