douchuoliu4422 2017-10-11 14:29
浏览 198
已采纳

加载更多而无需刷新页面[Wordpress]

I am using a WordPress plugin to load single post without refreshing the page, you can find it here: https://wordpress.org/plugins/read-more-without-refresh/

The plugin works perfectly fine when i use it's shortcode, it does exactly what i need it to do but here is the problem, I like to use it on all my post and this means i have to edit over 1000 post. Because i have some rules that i need to apply and there are some contents between the opening and closing shortcode elements , it gets tricky to use "do_shortcode"

Here is the shortcode: [read more="Click here to Read More" less="Read Less"] My Hidden Paragraphs Here [/read]

Here is what i want it to do: I need the plugin to apply the shortcode on all posts, after first 150 words and if the post has less than 150 words, it should do nothing.

Is there anyway i can do this? with php code or the already existing shortcode

  • 写回答

1条回答 默认 最新

  • douzen3516 2017-10-11 15:18
    关注

    You could probably use the_content filter but I haven't tested to see if it renders the shortcode or not.

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示