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()一樣,預設的是顯示標題。

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
檢視更多 >