get_the_title ( int|WP_Post $post )

Retrieve post title.

If the post is protected and the visitor is not an admin, then “Protected” will be displayed before the post title. If the post is private, then “Private” will be located before the post title.

Return: (string)

Source file: wp-includes/post-template.php

View source


Parameters

$post

(int|WP_Post) (Optional) Post ID or WP_Post object. Default is global $post.


Used by


Source

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 ) ) {

			/**
			 * Filter 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 ) {

			/**
			 * Filter 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 );
		}
	}

	/**
	 * Filter 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 );
}


Changelog

  • Since: WordPress 0.71

User Contributed Notes

  1. Print the current post’s title

    echo get_the_title();
    

    Simple breadcrumb trail for pages, two levels deep.

     
    echo '<div class="breadcrumb">';
    	// If there is a parent, display the link.
    	$parent_title = get_the_title( $post->post_parent );
    
    	if ( $parent_title != the_title( ' ', ' ', false ) ) {
    		echo '<a href="' . esc_url( get_permalink( $post->post_parent ) ) . '" alt="' . esc_attr( $parent_title ) . '">' . $parent_title . '</a> ยป ';
    	}
    
    	// Then go on to the current page link.
    	echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark" alt="' . esc_attr( the_title_attribute() ) . '">' . the_title() . '</a>';
    echo '</div>';
    

You must log in before being able to contribute a note.