如果您有权访问用户ID,则可以使用count_user_posts() 作用
您将获得如下用户的帖子数量:
//Assume the variable $thisUser is equal to a valid user ID
$ThisUserCount = count_user_posts($thisUser, \'recipe\');
编辑:在调用count\\u user\\u posts()函数的地方,传递的是数组$args,而不是post类型“recipe”。
<?php
// 1. We define the arguments to define what we want to recover
$args = array (
\'post_type\' => \'recipe\',
\'posts_per_page\' => \'16\',
);
// 2. We run the WP Query
// The Query
$the_query = new WP_Query ($args);
// 3. we display the number of messages and the authors!
// The Loop
if ($the_query-> have_posts()) {
//Set arguments to grab all authors with published recipes, and order them by user ID
$authorArgs = array(
\'orderby\' => \'ID\',
\'has_published_posts\' => array(\'recipe\'),
);
//Create an array of all authors with recipes published
$recipeAuthors = get_users($authorArgs);
//Loop through each recipe author
foreach($recipeAuthors as $user){
//Output user post count for recipes
echo count_user_posts($user->ID, \'recipe\');
echo \' recipes for \';
//Output user\'s display name
echo $user->display_name;
echo \'<br />\';
}
// 3. We launch the loop to display the articles and the authors!
// The Loop
echo \'<ul>\';
while ($the_query-> have_posts()) {
$the_query-> the_post();
echo \'<li>\'. get_the_title(). \'</li>\';
echo \'<li>\'. get_the_author(). \'</li>\';
}
echo \'</ul>\';
} else {
// no posts found
}
wp_reset_postdata ();?>
此外,get\\u the\\u author(2,$args)函数调用不正确。get\\u the\\u author()不再接受任何参数,它只返回循环中当前帖子的作者的显示名称。