在不刷新页面的情况下加载更多 [WordPress]

Load more without refreshing page [Wordpress]

我正在使用 WordPress 插件加载单个 post 而无需刷新页面,您可以在这里找到它:https://wordpress.org/plugins/read-more-without-refresh/

当我使用它的短代码时,该插件工作得非常好,它完全符合我的需要,但问题是,我喜欢在我所有的 post 上使用它,这意味着我必须编辑超过 1000 post。因为我有一些规则需要应用,并且 openingclosing 短代码元素之间有一些内容,所以使用 "do_shortcode"

这是简码:[read more="Click here to Read More" less="Read Less"] 我的隐藏段落在这里 [/read]

这是我想要它做的:我需要插件在所有 post 上应用短代码,在前 150 个单词之后,如果 post 少于 150 个单词,它应该什么都不做。

有什么办法可以做到吗?使用 php 代码或已经存在的短代码

您或许可以使用 the_content 过滤器,但我还没有测试它是否呈现简码。

function my_the_content_filter( $content ) {

    if ( is_single() ) {
        $word_count = str_word_count( strip_tags( $content ), 2 );

        if( count( $word_count ) >= 150 ) {
            $word = array_slice( $word_count, 150, 1, true );
            $word_pos = key( $word );
            $content = substr_replace( $content, '[read more="Click here to Read More" less="Read Less"]', $word_pos, 0 );
            $content .= '[/read]';
        }
    }

    return $content;
}

add_filter( 'the_content', 'my_the_content_filter' );