薇晓朵技术论坛 版块列表 入门与支持 功能改进 WordPress 代码段,可以显示当前文章作者的全部文章数量 通过简码调用

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

      依旧是为了方便,薇晓朵博客的目的是让用户投稿,所以我们这里添加下已经投稿的文章数量统计

      效果如下:

      function author_post_count_shortcode( $atts ) {
          $atts = shortcode_atts(
              array(
                  'author' => '',
              ),
              $atts
          );
      
          if ( ! empty( $atts['author'] ) ) {
              $author_id = $atts['author'];
          } else {
              $author_id = get_the_author_meta( 'ID' );
          }
      
          $args = array(
              'author'      => $author_id,
              'post_type'   => 'post',
              'post_status' => 'publish',
              'fields'      => 'ids',
              'posts_per_page' => -1,
          );
      
          $query = new WP_Query( $args );
          $count = $query->found_posts;
      
          return $count;
      }
      add_shortcode( 'author_post_count', 'author_post_count_shortcode' );

      使用方法:在文章或页面中,使用 [author_post_count] 简码调用即可显示当前文章作者的全部文章数量。可以使用 author 属性来指定特定的作者,例如 [author_post_count author=”2″] 将显示 ID 为 2 的作者的文章数量。

    正在查看 1 帖子:1-1 (共 1 个帖子)

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