問題描述
這是我的摘錄代碼。
// Generate custom excerpt length
function wpbx_excerpt_length($length) {
return 300;
}
add_filter('excerpt_length', 'wpbx_excerpt_length');
如何允許 html 像<a> <b> <i> <br>
最佳解決方案
完整的指南
我最近回答了一些有關摘錄的問題,所以我將盡可能詳細的説明。
前言
這個答案似乎有幾個問題,代碼應該在哪裏,答案是,這取決於你和你如何看待合適。有幾個選項可以放置代碼 (如果沒有明確説明):
- 在你的主題的 functions.php 或任何文件中用作函數文件。只要記住,當您執行此操作時,如果主題不是您自己的,則升級主題時,所有更改都將丟失
- 更好的方法是使用子主題中的代碼。如上所述,在 functions.php 或函數相關的文件中
- 使用插件中的代碼。這是首選的方式,因為這使得代碼可用於所有主題。如果您切換主題,則不用擔心重寫相同的代碼。
我希望這樣清理一下:-)
HTML 標籤/格式化
the_excerpt()首先不接受任何參數,所以沒有什麼可以傳遞給它。事實上,the_excerpt()將內容修剪為 55 個字,所有 HTML 標籤在返回文本之前都被剝離。 the_excerpt()位於 wp-includes/post-template.php 。要允許摘錄中的某些或所有 HTML 標籤,必須創建一個新的摘錄。
首先,原來的功能需要先刪除,然後新功能需要掛接到 get_the_excerpt 。請注意,這個新的摘錄仍然可以作為 the_excerpt()在模板文件中被調用,不需要改變。 get_the_excerpt()位於 wp-includes/post-template.php 。
摘錄使用 wp_trim_excerpt 返回修剪的文本,因此我們需要先從摘錄過濾器中刪除 wp_trim_excerpt 。 wp_trim_excerpt()位於 wp-includes/formatting.php,第 2355 行。這是如何:
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
您現在可以將您的新摘錄添加到 get_the_excerpt
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
為了允許 html 標籤/格式化,我們需要指定你需要允許的標籤。您可以使用以下 strip_tags 語句來實現
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags());
第二個參數 wpse_allowedtags()是一個小功能,用於添加標籤 the_excerpt()將允許。有關 HTML 5 標籤的完整列表,請查看 here 。這裏是功能,添加任何你需要允許/保持的 html 標籤
function wpse_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>';
}
如果您需要允許所有 HTML 標籤,即不剝離任何標籤,則可以完全省略/刪除 strips_tags()功能。
然而,要注意的是,當允許 html 標籤時,這些標籤被算作字詞,所以您的字數與片段和標籤的摘錄將不一樣。要糾正這一點,您需要首先從實際的字數中刪除這些標籤,以便僅對單詞進行計數。
我寫了一個摘錄,將允許所有的標籤,僅計數單詞作為單詞,並在設定的單詞數量之後完成一個句子 (所以文本不會被修剪 mid-sentence),並在最後一個單詞之後添加一個閲讀更多的文本。
這是完整的代碼
function wpse_allowedtags() {
// Add custom tags to this string
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>s]+)s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[,;?.!]s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' <a href="'.%20esc_url(%20get_permalink()%20)%20.%20'">' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
您可以從需要額外的功能中刪除’//’ 。
自定義長度
有時您需要顯示不同長度的簡單摘錄,並且為每個帖子/功能/頁面撰寫摘錄是不可行的。這是一個很好的小功能使用 wp_trim_words
function wpse_custom_excerpts($limit) {
return wp_trim_words(get_the_excerpt(), $limit, '<a href="'.%20esc_url(%20get_permalink()%20)%20.%20'">' . ' …' . __( 'Read more »', 'wpse' ) . '</a>');
}
這個小功能是採用 get_the_excerpt,將其修剪為用户設置的 $limit,並在結束時以更多的鏈接返回文本。
你可以按照你的模板來調用這個摘錄
echo wpse_custom_excerpts($limit);
其中 $limit 將是您的字數,所以 30 個詞的摘錄將
echo wpse_custom_excerpts(30);
只要一件事要記住,如果你將限制設置為更多的 55 個字,只有 55 個字將被歸還,因為摘錄長度只有 55 個字。如果需要更長的摘錄,請改用 get_the_content 。
自定義長度
如果只需要更改 the_excerpt()的長度,可以使用以下功能
function wpse_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'wpse_excerpt_length', 999 );
記住,您將需要設置大於 10 的優先級,以便在默認情況下執行自定義功能。
添加閲讀更多鏈接
摘錄中返回的所有文本都有可疑的 [...]。要在 hellips 的地方添加一個閲讀更多的文本,請使用此功能
function wpse_excerpt_more( $more ) {
return ' <a class="read-more" href="'.%20get_permalink(%20get_the_ID()%20)%20.%20'">' . __('Read More', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'wpse_excerpt_more' );
編輯
摘錄第一段
我想保持這個完整,所以這是在第一段之後修剪的摘錄
這是一個保持 HTML 標籤的功能,在摘錄末尾添加了一個 「閲讀更多」 鏈接,並在第一段之後修剪了摘錄。
if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) :
function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( '' == $wpse0001_excerpt ) {
$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );
$wpse0001_excerpt = str_replace(']]>', ']]>', $wpse0001_excerpt);
$excerpt_end = ' <a href="'.%20esc_url(%20get_permalink()%20)%20.%20'">' . ' » ' . sprintf(__( 'Read more about: %s »', 'pietergoosen' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;
return $wpse0001_excerpt;
}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');
編輯 29-10-2015
對於任何需要解決方法的人,如果節選時間短於設置的字數,則在摘錄後不顯示閲讀更多鏈接,請參閲以下問題和答案
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。