薇晓朵技术论坛 版块列表 入门与支持 功能改进 WordPress 代码段,显示文章的阅读时间 然后通过简码调用

正在查看 2 帖子:1-2 (共 2 个帖子)
  • 作者
    帖子
  • 诗语诗语
    管理员
      @feibisi
      文章数量:5689

      薇晓朵博客改版重启使用,需要严肃对待优质内容,所以把各方面的参数需要调整公布下,显示效果如下:

      效果如下

      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]

      诗语诗语
      管理员
        @feibisi
        文章数量:5689

        我们因为需要添加 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 个帖子)

      抱歉,回复话题必需登录。