WordPress 代碼段,顯示文章的閲讀時間 然後通過簡碼調用

  • 該話題包含 1 個回覆,1 人蔘與,最後由詩語 更新於 2 年前
正在查看 2 個帖子:1-2 (共 2 個帖子)
  • 作者
    帖子
  • 詩語
    管理員
    • 文章數量: 5,926
    @feibisi
    樓主

    薇曉朵博客改版重啓使用,需要嚴肅對待優質內容,所以把各方面的參數需要調整公佈下,顯示效果如下:

    效果如下

    function reading_time_shortcode( $atts ) {
        $post_id = get_the_ID();
        $post_content = get_post_field( 'post_content', $post_id );
        $word_count = preg_match_all( '/[\p{L}\'\-]+|\p{Han}+/u', $post_content, $matches );
        $reading_time = ceil( $word_count / 200 ); // Assumes an average reading speed of 200 words per minute
        return '<p> 預計閲讀時間:'.$reading_time.' 分鐘</p>';
    }
    add_shortcode( 'reading-time', 'reading_time_shortcode' );
    

    調用簡碼輸入 [reading-time]

    詩語
    管理員
    • 文章數量: 5,926
    @feibisi
    樓主

    我們因為需要添加 CSS 小圖標就給代碼加了個 class 如下

    function reading_time_shortcode( $atts ) {
        $post_id = get_the_ID();
        $post_content = get_post_field( 'post_content', $post_id );
        $word_count = preg_match_all( '/[\p{L}\'\-]+|\p{Han}+/u', $post_content, $matches );
        $reading_time = ceil( $word_count / 150 ); // Assumes an average reading speed of 200 words per minute
        return '<p class="reading-time"> 預計閲讀時間:'.$reading_time.' 分鐘</p>';
    }
    add_shortcode( 'reading-time', 'reading_time_shortcode' );

    添加到需要顯示的地方就行。

正在查看 2 個帖子:1-2 (共 2 個帖子)
  • 哎呀,回覆話題必需登錄。

話題信息

  • 當前位於:功能改進
  • 1 條回覆
  • 1 個參與人
  • 最後回覆:<a href="https://bbs.weixiaoduo.com/users/feibisi/" title=" 查看詩語的個人資料" class="bbp-author-link"><span class="bbp-author-name"> 詩語</span></a>
  • 上次活動:<a href="https://bbs.weixiaoduo.com/topic/41954/#post-41955" title=" 回覆至:WordPress 代碼段,顯示文章的閲讀時間 然後通過簡碼調用">2 年前</a>