問題描述

這是我的摘錄程式碼。

// 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_excerptwp_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(']]>', ']]&gt;', $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'">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', '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'">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', '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(']]>', ']]&gt;', $wpse0001_excerpt);

            $excerpt_end = ' <a href="'.%20esc_url(%20get_permalink()%20)%20.%20'">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', '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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。