WordPress 代码段,可以显示当前文章作者的全部文章数量 通过简码调用

WordPress 代码段,可以显示当前文章作者的全部文章数量 通过简码调用


正在查看 1 个帖子:1-1 (共 1 个帖子)
  • 作者
    帖子
  • 诗语
    管理员
    • 文章数量: 5,865
    @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 个帖子)
  • 哎呀,回复话题必需登录。

未找到对应子版块?请直接在 [疑难杂症] 版块中发帖提问。

收藏纪念品


有奖活动

参与论坛社区问答,有机会获赠文派瓦普(Wapuu.com)手办模型。

话题信息

商业系统