返回介绍

the_title_attribute()

发布于 2017-09-11 10:40:46 字数 3887 浏览 940 评论 0 收藏 0

the_title_attribute( string|array $args = '' )

Sanitize the current title when retrieving or displaying.


description

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;
	}
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

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
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文