Wordpress Word Count排除短代码?

时间:2014-03-18 18:01:57

标签: php wordpress wordpress-plugin wordpress-theming

我有一个功能可以获得帖子中的整体字数。

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]

关于如何将该部分添加到上述函数的任何想法?

1 个答案:

答案 0 :(得分:0)

WordPress有一个方便的功能,strip_shortcodes。法典详细信息:https://codex.wordpress.org/Function_Reference/strip_shortcodes

您的函数使用$post->ID,但您没有获得全局帖子值。

最后,您已经可以访问全局$ post对象中的帖子内容,所以只需使用它而不是get_post_field。

E.g。 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 ) ) );
}
相关问题