the_title_attribute() - WordPress 函数参考中文文档

返回介绍

the_title_attribute()

发布于 2017-09-11 字数3889 浏览 869 评论 0

the_title_attribute( string|array $args = '' )

Sanitize the current title when retrieving or displaying.


描述

Works like 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 esc_attr() before it is passed to the user or displayed. The default as with the_title(), is to display the title.


参数

$args

(string|array)
(Optional)
Title attribute arguments. Optional.

  • ‘before’
    (string) Markup to prepend to the title.
  • ‘after’
    (string) Markup to append to the title.
  • ‘echo’
    (bool) Whether to echo or return the title. Default true for echo.
  • ‘post’
    (WP_Post) Current post object to retrieve the title for.

Default value: ”


返回值

(string|void) String when echo is false.


源代码

File: wp-includes/post-template.php

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 ) {
		return;
	}

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

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

更新日志

Version 描述
2.3.0 Introduced.

相关函数

Uses

  • wp-includes/formatting.php:
    esc_attr()
  • wp-includes/functions.php:
    wp_parse_args()
  • wp-includes/post-template.php:
    get_the_title()
  • wp-includes/post.php:
    get_post()

Used By

  • wp-includes/general-template.php:
    feed_links_extra()
  • wp-includes/link-template.php:
    the_shortlink()
  • wp-includes/link-template.php:
    get_adjacent_post_rel_link()
  • wp-includes/post-template.php:
    get_the_content()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    PHP with text args

    
    printf(	'<a href="%s" title="%s">%s</a>',
    	get_permalink(),
    	the_title_attribute( 'echo=0' ),
    	get_the_title()
    );
    
  2. Inline with array args

    
    <a href="<?php the_permalink(); ?>" 
    	title="<?php the_title_attribute( array(
    		'before' => 'Permalink to: ',
    		'after'  => ''
    	) ); ?>">
    	<?php the_title(); ?>
    </a>
    

    Inline with text args

    
    /**
     * Output the post title.
     */
    function wpdocs_do_post_title() {
    	?>
    	<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( 'before=Permalink to: "&after="' ); ?>"><?php the_title(); ?></a>
    	<?php
    }
    

上一篇:the_title()

下一篇:the_title_rss()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!