3 * Author Template functions for use in themes.
5 * These functions must be used within the WordPress Loop.
7 * @link http://codex.wordpress.org/Author_Templates
10 * @subpackage Template
14 * Retrieve the author of the current post.
17 * @uses $authordata The current author's DB object.
18 * @uses apply_filters() Calls 'the_author' hook on the author display name.
20 * @param string $deprecated Deprecated.
21 * @return string The author's display name.
23 function get_the_author($deprecated = '') {
26 if ( !empty( $deprecated ) )
27 _deprecated_argument( __FUNCTION__, '2.1' );
29 return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null);
33 * Display the name of the author of the current post.
35 * The behavior of this function is based off of old functionality predating
36 * get_the_author(). This function is not deprecated, but is designed to echo
37 * the value from get_the_author() and as an result of any old theme that might
38 * still use the old behavior will also pass the value from get_the_author().
40 * The normal, expected behavior of this function is to echo the author and not
41 * return it. However, backwards compatiability has to be maintained.
44 * @see get_the_author()
45 * @link http://codex.wordpress.org/Template_Tags/the_author
47 * @param string $deprecated Deprecated.
48 * @param string $deprecated_echo Deprecated. Use get_the_author(). Echo the string or return it.
49 * @return string The author's display name, from get_the_author().
51 function the_author( $deprecated = '', $deprecated_echo = true ) {
52 if ( !empty( $deprecated ) )
53 _deprecated_argument( __FUNCTION__, '2.1' );
54 if ( $deprecated_echo !== true )
55 _deprecated_argument( __FUNCTION__, '1.5', __('Use <code>get_the_author()</code> instead if you do not want the value echoed.') );
56 if ( $deprecated_echo )
57 echo get_the_author();
58 return get_the_author();
62 * Retrieve the author who last edited the current post.
65 * @uses $post The current post's DB object.
66 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
67 * @uses get_userdata() Retrieves the author's DB object.
68 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
69 * @return string The author's display name.
71 function get_the_modified_author() {
73 if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
74 $last_user = get_userdata($last_id);
75 return apply_filters('the_modified_author', $last_user->display_name);
80 * Display the name of the author who last edited the current post.
83 * @see get_the_author()
84 * @return string The author's display name, from get_the_modified_author().
86 function the_modified_author() {
87 echo get_the_modified_author();
91 * Retrieve the requested data of the author of the current post.
92 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
94 * @uses $authordata The current author's DB object (if $user_id not specified).
95 * @param string $field selects the field of the users record.
96 * @param int $user_id Optional. User ID.
97 * @return string The author's field from the current author's DB object.
99 function get_the_author_meta($field = '', $user_id = false) {
103 $authordata = get_userdata( $user_id );
105 $field = strtolower($field);
106 $user_field = "user_$field";
108 if ( 'id' == $field )
109 $value = isset($authordata->ID) ? (int)$authordata->ID : 0;
110 elseif ( isset($authordata->$user_field) )
111 $value = $authordata->$user_field;
113 $value = isset($authordata->$field) ? $authordata->$field : '';
115 return apply_filters('get_the_author_' . $field, $value, $user_id);
119 * Retrieve the requested data of the author of the current post.
120 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
122 * @param string $field selects the field of the users record.
123 * @param int $user_id Optional. User ID.
124 * @echo string The author's field from the current author's DB object.
126 function the_author_meta($field = '', $user_id = false) {
127 echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id);
131 * Retrieve either author's link or author's name.
133 * If the author has a home page set, return an HTML link, otherwise just return the
136 * @uses get_the_author_meta()
137 * @uses get_the_author()
139 function get_the_author_link() {
140 if ( get_the_author_meta('url') ) {
141 return '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ) . '" rel="external">' . get_the_author() . '</a>';
143 return get_the_author();
148 * Display either author's link or author's name.
150 * If the author has a home page set, echo an HTML link, otherwise just echo the
153 * @link http://codex.wordpress.org/Template_Tags/the_author_link
155 * @uses get_the_author_link()
157 function the_author_link() {
158 echo get_the_author_link();
162 * Retrieve the number of posts by the author of the current post.
165 * @uses $post The current post in the Loop's DB object.
166 * @uses count_user_posts()
167 * @return int The number of posts by the author.
169 function get_the_author_posts() {
171 return count_user_posts($post->post_author);
175 * Display the number of posts by the author of the current post.
177 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
179 * @uses get_the_author_posts() Echoes returned value from function.
181 function the_author_posts() {
182 echo get_the_author_posts();
186 * Display an HTML link to the author page of the author of the current post.
188 * Does just echo get_author_posts_url() function, like the others do. The
189 * reason for this, is that another function is used to help in printing the
190 * link to the author's posts.
192 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
194 * @uses $authordata The current author's DB object.
195 * @uses get_author_posts_url()
196 * @uses get_the_author()
197 * @param string $deprecated Deprecated.
199 function the_author_posts_link($deprecated = '') {
200 if ( !empty( $deprecated ) )
201 _deprecated_argument( __FUNCTION__, '2.1' );
205 '<a href="%1$s" title="%2$s">%3$s</a>',
206 get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
207 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ),
210 echo apply_filters( 'the_author_posts_link', $link );
214 * Retrieve the URL to the author page of the author of the current post.
217 * @uses $wp_rewrite WP_Rewrite
218 * @return string The URL to the author's page.
220 function get_author_posts_url($author_id, $author_nicename = '') {
222 $auth_ID = (int) $author_id;
223 $link = $wp_rewrite->get_author_permastruct();
225 if ( empty($link) ) {
226 $file = home_url() . '/';
227 $link = $file . '?author=' . $auth_ID;
229 if ( '' == $author_nicename ) {
230 $user = get_userdata($author_id);
231 if ( !empty($user->user_nicename) )
232 $author_nicename = $user->user_nicename;
234 $link = str_replace('%author%', $author_nicename, $link);
235 $link = home_url() . trailingslashit($link);
238 $link = apply_filters('author_link', $link, $author_id, $author_nicename);
244 * List all the authors of the blog, with several options available.
247 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
248 * author's name.</li>
249 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
250 * installed bydefault.</li>
251 * <li>show_fullname (boolean) (false): Show their full names.</li>
252 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
253 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
254 * <li>feed_image (string) (''): If isn't empty, use this image to link to
256 * <li>echo (boolean) (true): Set to false to return the output, instead of
258 * <li>style (string) ('list'): Whether to display list of authors in list form
259 * or as a string.</li>
260 * <li>html (bool) (true): Whether to list the items in html for or plaintext.
264 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
266 * @param array $args The argument array.
267 * @return null|string The output, if echo is set to false.
269 function wp_list_authors($args = '') {
273 'optioncount' => false, 'exclude_admin' => true,
274 'show_fullname' => false, 'hide_empty' => true,
275 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
276 'style' => 'list', 'html' => true
279 $r = wp_parse_args( $args, $defaults );
280 extract($r, EXTR_SKIP);
283 /** @todo Move select to get_authors(). */
284 $users = get_users_of_blog();
285 $author_ids = array();
286 foreach ( (array) $users as $user )
287 $author_ids[] = $user->user_id;
288 if ( count($author_ids) > 0 ) {
289 $author_ids = implode(',', $author_ids );
290 $authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
295 $author_count = array();
296 foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
297 $author_count[$row->post_author] = $row->count;
299 foreach ( (array) $authors as $author ) {
303 $author = get_userdata( $author->ID );
304 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
305 $name = $author->display_name;
307 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
308 $name = "$author->first_name $author->last_name";
313 $return .= $name . ', ';
315 $return .= $name . ', ';
317 // No need to go further to process HTML.
321 if ( !($posts == 0 && $hide_empty) && 'list' == $style )
327 $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>';
329 if ( (! empty($feed_image)) || (! empty($feed)) ) {
331 if (empty($feed_image))
333 $link .= '<a href="' . get_author_feed_link($author->ID) . '"';
335 if ( !empty($feed) ) {
336 $title = ' title="' . esc_attr($feed) . '"';
337 $alt = ' alt="' . esc_attr($feed) . '"';
344 if ( !empty($feed_image) )
345 $link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />';
351 if ( empty($feed_image) )
356 $link .= ' ('. $posts . ')';
360 if ( $posts || ! $hide_empty )
361 $return .= $link . ( ( 'list' == $style ) ? '</li>' : ', ' );
364 $return = trim($return, ', ');