这里有一个我碰巧用过的。函数返回一个数组,该数组由作者ID数组、作者名称数组和作者数组成。
将此代码放入functions.php
-
/**
* Return an array of the number of posting authors, and their names and ID\'s
*/
function get_author_data(){
global $wpdb;
/** Create the query and then grab all of the ID\'s of all authors who have posted at least once */
$query = $wpdb->prepare(\'SELECT DISTINCT %1$s.post_author FROM %1$s WHERE %1$s.post_status IN ("publish")\', $wpdb->posts);
$author_ids = $wpdb->get_col($query);
$authors[\'ID\'] = $author_ids;
/** Get the name of all of the authors that have posted at least once */
$authors[\'name\'] = array();
if(!empty($authors[\'ID\'])) : foreach($authors[\'ID\'] as $author_id) :
$query = $wpdb->prepare(\'SELECT %1$s.display_name FROM %1$s WHERE %1$s.ID = %2$s\', $wpdb->users, $author_id);
$authors[\'name\'][] = $wpdb->get_var($query);
endforeach;
endif;
/** Get a count of the number of authors who have posted at least once */
$authors[\'count\'] = count($authors[\'ID\']);
return $authors;
}
当您想要获取信息时-
$authors = get_author_data();
然后输出所需的信息-
/** Output all author names */
if(!empty($authors[\'name\'])) : foreach($authors[\'name\'] as $author_nam) :
print_f(\'<li>%1$s</li>\', $author_name);
endforeach;
endif;
/** Output a count of authors */
print_f(\'<p>Number of contributing authors: %1$s</p>\', $authors[\'count\']);