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>