Displays or returns the title of the current post. It somewhat duplicates the functionality of the_title(), but provides a “clean” version of the title for use in HTML attributes by stripping HTML tags with strip_tags() and by converting certain characters (including quotes) to their character entity equivalent with esc_attr(); it also uses query-string style parameters. This tag must be within The Loop.

the_title_attribute() 描述


 <?php the_title_attribute$args ); ?> 

the_title_attribute() 用法



(string) (可选) Text to place before the title.

默认值: None


(string) (可选) Text to place after the title.

默认值: None


(Boolean) (可选) Display the title (TRUE) or return it for use in PHP (FALSE).

默认值: TRUE


(integer or object) (可选) The ID of the post or an object that specifies the post from which to get the title. By default the current post is fetched.

默认值: null

the_title_attribute() 参数



  • 添加于 版本: 2.3.0

the_title_attribute() 历史


the_title_attribute() 函数的代码位于 wp-includes/post-template.php.

/* ----------------------------------
 * wordpress之魂 ©
 * ---------------------------------- */
 * Sanitize the current title when retrieving or displaying.
 * Works like {@link the_title()}, except the parameters can be in a string or
 * an array. See the function for what can be override in the $args parameter.
 * The title before it is displayed will have the tags stripped and {@link
 * esc_attr()} before it is passed to the user or displayed. The default
 * as with {@link the_title()}, is to display the title.
 * @since 2.3.0
 * @param string|array $args {
 *     Title attribute arguments. Optional.
 *     @type string  $before Markup to prepend to the title. Default empty.
 *     @type string  $after  Markup to append to the title. Default empty.
 *     @type bool    $echo   Whether to echo or return the title. Default true for echo.
 *     @type WP_Post $post   Current post object to retrieve the title for.
 * }
 * @return string|void String when echo is false.
function the_title_attribute( $args = '' ) {
	$defaults = array( 'before' => '', 'after' =>  '', 'echo' => true, 'post' => get_post() );
	$r = wp_parse_args( $args, $defaults );

	$title = get_the_title( $r['post'] );

	if ( strlen( $title ) == 0 ) {

	$title = $r['before'] . $title . $r['after'];
	$title = esc_attr( strip_tags( $title ) );

	if ( $r['echo'] ) {
		echo $title;
	} else {
		return $title;

the_title_attribute() 源文件