簡體   English   中英

WordPress的字數統計,以排除短代碼?

[英]Wordpress Word Count to exclude shortcodes?

我有一個功能,可以獲取帖子中的總字數。

function ic_word_count() {
    global $post;

    $ic_content = strip_tags(  $post->post_content );
    $ic_stripped = strip_shortcodes($ic_content);
    return $ic_stripped;
}

我試圖讓它排除所有短代碼,所以基本上排除任何帶有方括號的內容以及它們之間的所有內容。 例如排除[shortcode][This Shortcode]

關於如何將該部分添加到上述功能的任何想法嗎?

WordPress有一個方便的功能,strip_shortcodes。 此處的Codex詳細信息: https : //codex.wordpress.org/Function_Reference/strip_shortcodes

您的函數使用$post->ID但沒有獲得全局的post值。

最后,您已經可以訪問全局$ post對象內部的帖子內容,因此只需使用它代替get_post_field。

例如, global $post;

function ic_word_count() {
    global $post;

    $wc_content = $post->post_content;
    $ic_word_count = str_word_count( strip_tags( strip_shortcodes( $wc_content ) ) );

    return $ic_word_count;
}

較小的版本:

function ic_word_count() {
    global $post;

    return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM