wp_html_excerpt

函式
wp_html_excerpt ( $str, $count, $more = null )
引數
  • (string) $str String to get the excerpt from.
    Required:
  • (int) $count Maximum number of characters to take.
    Required:
  • (string) $more Optional. What to append if $str needs to be trimmed. Defaults to empty string.
    Required:
    Default: null
返回值
  • (string) The excerpt.
定義位置
相關方法
wp_trim_excerptwp_html_splithas_excerptthe_excerptwp_embed_excerpt_more
引入
2.5.0
棄用
-

wp_html_excerpt: 這個函式用於在保留標籤的同時截斷HTML內容。它把內容、長度和更多的文字作為引數,並返回截斷後的內容。

安全地從HTML字串中提取不超過前$count的字元。

UTF-8,標籤和實體安全提取字首。裡面的實體將*NOT*被算作一個字元。例如,&會被算作4,<會被算作3,等等。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_html_excerpt( $str, $count, $more = null ) {
if ( null === $more ) {
$more = '';
}
$str = wp_strip_all_tags( $str, true );
$excerpt = mb_substr( $str, 0, $count );
// Remove part of an entity at the end.
$excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt );
if ( $str != $excerpt ) {
$excerpt = trim( $excerpt ) . $more;
}
return $excerpt;
}
function wp_html_excerpt( $str, $count, $more = null ) { if ( null === $more ) { $more = ''; } $str = wp_strip_all_tags( $str, true ); $excerpt = mb_substr( $str, 0, $count ); // Remove part of an entity at the end. $excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt ); if ( $str != $excerpt ) { $excerpt = trim( $excerpt ) . $more; } return $excerpt; }
function wp_html_excerpt( $str, $count, $more = null ) {
	if ( null === $more ) {
		$more = '';
	}

	$str     = wp_strip_all_tags( $str, true );
	$excerpt = mb_substr( $str, 0, $count );

	// Remove part of an entity at the end.
	$excerpt = preg_replace( '/&[^;s]{0,6}$/', '', $excerpt );
	if ( $str != $excerpt ) {
		$excerpt = trim( $excerpt ) . $more;
	}

	return $excerpt;
}

常見問題

FAQs
檢視更多 >