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

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 的作者的文章數量。