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 = '') {
25 return apply_filters('the_author', $authordata->display_name);
29 * Display the name of the author of the current post.
31 * The behavior of this function is based off of old functionality predating
32 * get_the_author(). This function is not deprecated, but is designed to echo
33 * the value from get_the_author() and as an result of any old theme that might
34 * still use the old behavior will also pass the value from get_the_author().
36 * The normal, expected behavior of this function is to echo the author and not
37 * return it. However, backwards compatiability has to be maintained.
40 * @see get_the_author()
41 * @link http://codex.wordpress.org/Template_Tags/the_author
43 * @param string $deprecated Deprecated.
44 * @param string $deprecated_echo Echo the string or return it.
45 * @return string The author's display name, from get_the_author().
47 function the_author($deprecated = '', $deprecated_echo = true) {
48 if ( $deprecated_echo )
49 echo get_the_author();
50 return get_the_author();
54 * Retrieve the author who last edited the current post.
57 * @uses $post The current post's DB object.
58 * @uses get_post_meta() Retrieves the ID of the author who last edited the current post.
59 * @uses get_userdata() Retrieves the author's DB object.
60 * @uses apply_filters() Calls 'the_modified_author' hook on the author display name.
61 * @return string The author's display name.
63 function get_the_modified_author() {
65 if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
66 $last_user = get_userdata($last_id);
67 return apply_filters('the_modified_author', $last_user->display_name);
72 * Display the name of the author who last edited the current post.
75 * @see get_the_author()
76 * @return string The author's display name, from get_the_modified_author().
78 function the_modified_author() {
79 echo get_the_modified_author();
83 * Retrieve the requested data of the author of the current post.
84 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
86 * @uses $authordata The current author's DB object (if $user_id not specified).
87 * @param string $field selects the field of the users record.
88 * @param int $user_id Optional. User ID.
89 * @return string The author's field from the current author's DB object.
91 function get_the_author_meta($field = '', $user_id = false) {
95 $authordata = get_userdata( $user_id );
97 $field = strtolower($field);
98 $user_field = "user_$field";
100 if ( 'id' == $field )
101 $value = isset($authordata->ID) ? (int)$authordata->ID : 0;
102 elseif ( isset($authordata->$user_field) )
103 $value = $authordata->$user_field;
105 $value = isset($authordata->$field) ? $authordata->$field : '';
107 return apply_filters('get_the_author_' . $field, $value);
111 * Retrieve the requested data of the author of the current post.
112 * @link http://codex.wordpress.org/Template_Tags/the_author_meta
114 * @param string $field selects the field of the users record.
115 * @param int $user_id Optional. User ID.
116 * @echo string The author's field from the current author's DB object.
118 function the_author_meta($field = '', $user_id = false) {
119 echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id));
123 * Display either author's link or author's name.
125 * If the author has a home page set, echo an HTML link, otherwise just echo the
128 * @link http://codex.wordpress.org/Template_Tags/the_author_link
130 * @uses get_the_author_meta()
133 function the_author_link() {
134 if ( get_the_author_meta('url') ) {
135 echo '<a href="' . get_the_author_meta('url') . '" title="' . sprintf(__("Visit %s’s website"), get_the_author()) . '" rel="external">' . get_the_author() . '</a>';
142 * Retrieve the number of posts by the author of the current post.
145 * @uses $post The current post in the Loop's DB object.
146 * @uses get_usernumposts()
147 * @return int The number of posts by the author.
149 function get_the_author_posts() {
151 return get_usernumposts($post->post_author);
155 * Display the number of posts by the author of the current post.
157 * @link http://codex.wordpress.org/Template_Tags/the_author_posts
159 * @uses get_the_author_posts() Echos returned value from function.
161 function the_author_posts() {
162 echo get_the_author_posts();
166 * Display an HTML link to the author page of the author of the current post.
168 * Does just echo get_author_posts_url() function, like the others do. The
169 * reason for this, is that another function is used to help in printing the
170 * link to the author's posts.
172 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link
174 * @uses $authordata The current author's DB object.
175 * @uses get_author_posts_url()
176 * @uses get_the_author()
177 * @param string $deprecated Deprecated.
179 function the_author_posts_link($deprecated = '') {
182 '<a href="%1$s" title="%2$s">%3$s</a>',
183 get_author_posts_url( $authordata->ID, $authordata->user_nicename ),
184 sprintf( __( 'Posts by %s' ), esc_attr( get_the_author() ) ),
190 * Retrieve the URL to the author page of the author of the current post.
193 * @uses $wp_rewrite WP_Rewrite
194 * @return string The URL to the author's page.
196 function get_author_posts_url($author_id, $author_nicename = '') {
198 $auth_ID = (int) $author_id;
199 $link = $wp_rewrite->get_author_permastruct();
201 if ( empty($link) ) {
202 $file = get_option('home') . '/';
203 $link = $file . '?author=' . $auth_ID;
205 if ( '' == $author_nicename ) {
206 $user = get_userdata($author_id);
207 if ( !empty($user->user_nicename) )
208 $author_nicename = $user->user_nicename;
210 $link = str_replace('%author%', $author_nicename, $link);
211 $link = get_option('home') . trailingslashit($link);
214 $link = apply_filters('author_link', $link, $author_id, $author_nicename);
220 * List all the authors of the blog, with several options available.
223 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the
224 * author's name.</li>
225 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is
226 * installed bydefault.</li>
227 * <li>show_fullname (boolean) (false): Show their full names.</li>
228 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li>
229 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li>
230 * <li>feed_image (string) (''): If isn't empty, use this image to link to
232 * <li>echo (boolean) (true): Set to false to return the output, instead of
234 * <li>style (string) ('list'): Whether to display list of authors in list form
235 * or as a string.</li>
236 * <li>html (bool) (true): Whether to list the items in html for or plaintext.
240 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors
242 * @param array $args The argument array.
243 * @return null|string The output, if echo is set to false.
245 function wp_list_authors($args = '') {
249 'optioncount' => false, 'exclude_admin' => true,
250 'show_fullname' => false, 'hide_empty' => true,
251 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
252 'style' => 'list', 'html' => true
255 $r = wp_parse_args( $args, $defaults );
256 extract($r, EXTR_SKIP);
259 /** @todo Move select to get_authors(). */
260 $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name");
262 $author_count = array();
263 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) {
264 $author_count[$row->post_author] = $row->count;
267 foreach ( (array) $authors as $author ) {
271 $author = get_userdata( $author->ID );
272 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
273 $name = $author->display_name;
275 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
276 $name = "$author->first_name $author->last_name";
281 $return .= $name . ', ';
283 $return .= $name . ', ';
285 // No need to go further to process HTML.
289 if ( !($posts == 0 && $hide_empty) && 'list' == $style )
295 $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . sprintf(__("Posts by %s"), esc_attr($author->display_name)) . '">' . $name . '</a>';
297 if ( (! empty($feed_image)) || (! empty($feed)) ) {
299 if (empty($feed_image))
301 $link .= '<a href="' . get_author_feed_link($author->ID) . '"';
303 if ( !empty($feed) ) {
304 $title = ' title="' . $feed . '"';
305 $alt = ' alt="' . $feed . '"';
312 if ( !empty($feed_image) )
313 $link .= "<img src=\"$feed_image\" style=\"border: none;\"$alt$title" . ' />';
319 if ( empty($feed_image) )
324 $link .= ' ('. $posts . ')';
328 if ( !($posts == 0 && $hide_empty) && 'list' == $style )
329 $return .= $link . '</li>';
330 else if ( ! $hide_empty )
331 $return .= $link . ', ';
334 $return = trim($return, ', ');