the_title_attribute

函数
the_title_attribute ( $args = '' )
参数
  • (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. }
    Required:
    Default: (empty)
返回值
  • (void|string) Void if 'echo' argument is true, the title attribute if 'echo' is false.
定义位置
相关方法
the_title_rssget_language_attributesthe_titlelanguage_attributeswp_filter_oembed_iframe_title_attribute
引入
2.3.0
弃用
-

the_title_attribute: 这个函数检索当前文章或页面的标题属性。它被用来给围绕着文章或页面标题的HTML锚标签添加标题属性。

在检索或显示时对当前标题进行净化。

工作原理与the_title()类似,只是参数可以是字符串或数组。关于$args参数中可以覆盖的内容,请参见该函数。

在显示之前,标题将被剥离标签,并在传递给用户或显示之前被esc_attr()。与the_title()一样,默认的是显示标题。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function the_title_attribute( $args = '' ) {
$defaults = array(
'before' => '',
'after' => '',
'echo' => true,
'post' => get_post(),
);
$parsed_args = wp_parse_args( $args, $defaults );
$title = get_the_title( $parsed_args['post'] );
if ( strlen( $title ) == 0 ) {
return;
}
$title = $parsed_args['before'] . $title . $parsed_args['after'];
$title = esc_attr( strip_tags( $title ) );
if ( $parsed_args['echo'] ) {
echo $title;
} else {
return $title;
}
}
function the_title_attribute( $args = '' ) { $defaults = array( 'before' => '', 'after' => '', 'echo' => true, 'post' => get_post(), ); $parsed_args = wp_parse_args( $args, $defaults ); $title = get_the_title( $parsed_args['post'] ); if ( strlen( $title ) == 0 ) { return; } $title = $parsed_args['before'] . $title . $parsed_args['after']; $title = esc_attr( strip_tags( $title ) ); if ( $parsed_args['echo'] ) { echo $title; } else { return $title; } }
function the_title_attribute( $args = '' ) {
	$defaults    = array(
		'before' => '',
		'after'  => '',
		'echo'   => true,
		'post'   => get_post(),
	);
	$parsed_args = wp_parse_args( $args, $defaults );

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

	if ( strlen( $title ) == 0 ) {
		return;
	}

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

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

常见问题

FAQs
查看更多 >