WordPress 代碼段,可以顯示當前文章作者的全部文章數量 通過簡碼調用

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

    依舊是為了方便,薇曉朵博客的目的是讓用户投稿,所以我們這裏添加下已經投稿的文章數量統計

    效果如下:

    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 個帖子)
  • 哎呀,回覆話題必需登錄。

話題信息

  • 當前位於:功能改進
  • 0 條回覆
  • 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/41957/" title="WordPress 代碼段,可以顯示當前文章作者的全部文章數量 通過簡碼調用">2 年前</a>