|
1 <?php |
|
2 /** |
|
3 * Author Template functions for use in themes. |
|
4 * |
|
5 * These functions must be used within the WordPress Loop. |
|
6 * |
|
7 * @link http://codex.wordpress.org/Author_Templates |
|
8 * |
|
9 * @package WordPress |
|
10 * @subpackage Template |
|
11 */ |
|
12 |
|
13 /** |
|
14 * Retrieve the author of the current post. |
|
15 * |
|
16 * @since 1.5 |
|
17 * @uses $authordata The current author's DB object. |
|
18 * @uses apply_filters() Calls 'the_author' hook on the author display name. |
|
19 * |
|
20 * @param string $deprecated Deprecated. |
|
21 * @return string The author's display name. |
|
22 */ |
|
23 function get_the_author($deprecated = '') { |
|
24 global $authordata; |
|
25 return apply_filters('the_author', is_object($authordata) ? $authordata->display_name : null); |
|
26 } |
|
27 |
|
28 /** |
|
29 * Display the name of the author of the current post. |
|
30 * |
|
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(). |
|
35 * |
|
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. |
|
38 * |
|
39 * @since 0.71 |
|
40 * @see get_the_author() |
|
41 * @link http://codex.wordpress.org/Template_Tags/the_author |
|
42 * |
|
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(). |
|
46 */ |
|
47 function the_author($deprecated = '', $deprecated_echo = true) { |
|
48 if ( $deprecated_echo ) |
|
49 echo get_the_author(); |
|
50 return get_the_author(); |
|
51 } |
|
52 |
|
53 /** |
|
54 * Retrieve the author who last edited the current post. |
|
55 * |
|
56 * @since 2.8 |
|
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. |
|
62 */ |
|
63 function get_the_modified_author() { |
|
64 global $post; |
|
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); |
|
68 } |
|
69 } |
|
70 |
|
71 /** |
|
72 * Display the name of the author who last edited the current post. |
|
73 * |
|
74 * @since 2.8 |
|
75 * @see get_the_author() |
|
76 * @return string The author's display name, from get_the_modified_author(). |
|
77 */ |
|
78 function the_modified_author() { |
|
79 echo get_the_modified_author(); |
|
80 } |
|
81 |
|
82 /** |
|
83 * Retrieve the requested data of the author of the current post. |
|
84 * @link http://codex.wordpress.org/Template_Tags/the_author_meta |
|
85 * @since 2.8.0 |
|
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. |
|
90 */ |
|
91 function get_the_author_meta($field = '', $user_id = false) { |
|
92 if ( ! $user_id ) |
|
93 global $authordata; |
|
94 else |
|
95 $authordata = get_userdata( $user_id ); |
|
96 |
|
97 $field = strtolower($field); |
|
98 $user_field = "user_$field"; |
|
99 |
|
100 if ( 'id' == $field ) |
|
101 $value = isset($authordata->ID) ? (int)$authordata->ID : 0; |
|
102 elseif ( isset($authordata->$user_field) ) |
|
103 $value = $authordata->$user_field; |
|
104 else |
|
105 $value = isset($authordata->$field) ? $authordata->$field : ''; |
|
106 |
|
107 return apply_filters('get_the_author_' . $field, $value, $user_id); |
|
108 } |
|
109 |
|
110 /** |
|
111 * Retrieve the requested data of the author of the current post. |
|
112 * @link http://codex.wordpress.org/Template_Tags/the_author_meta |
|
113 * @since 2.8.0 |
|
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. |
|
117 */ |
|
118 function the_author_meta($field = '', $user_id = false) { |
|
119 echo apply_filters('the_author_' . $field, get_the_author_meta($field, $user_id), $user_id); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Display either author's link or author's name. |
|
124 * |
|
125 * If the author has a home page set, echo an HTML link, otherwise just echo the |
|
126 * author's name. |
|
127 * |
|
128 * @link http://codex.wordpress.org/Template_Tags/the_author_link |
|
129 * @since 2.1 |
|
130 * @uses get_the_author_meta() |
|
131 * @uses the_author() |
|
132 */ |
|
133 function the_author_link() { |
|
134 if ( get_the_author_meta('url') ) { |
|
135 echo '<a href="' . get_the_author_meta('url') . '" title="' . esc_attr( sprintf(__("Visit %s’s website"), get_the_author()) ) . '" rel="external">' . get_the_author() . '</a>'; |
|
136 } else { |
|
137 the_author(); |
|
138 } |
|
139 } |
|
140 |
|
141 /** |
|
142 * Retrieve the number of posts by the author of the current post. |
|
143 * |
|
144 * @since 1.5 |
|
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. |
|
148 */ |
|
149 function get_the_author_posts() { |
|
150 global $post; |
|
151 return get_usernumposts($post->post_author); |
|
152 } |
|
153 |
|
154 /** |
|
155 * Display the number of posts by the author of the current post. |
|
156 * |
|
157 * @link http://codex.wordpress.org/Template_Tags/the_author_posts |
|
158 * @since 0.71 |
|
159 * @uses get_the_author_posts() Echos returned value from function. |
|
160 */ |
|
161 function the_author_posts() { |
|
162 echo get_the_author_posts(); |
|
163 } |
|
164 |
|
165 /** |
|
166 * Display an HTML link to the author page of the author of the current post. |
|
167 * |
|
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. |
|
171 * |
|
172 * @link http://codex.wordpress.org/Template_Tags/the_author_posts_link |
|
173 * @since 1.2.0 |
|
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. |
|
178 */ |
|
179 function the_author_posts_link($deprecated = '') { |
|
180 global $authordata; |
|
181 $link = sprintf( |
|
182 '<a href="%1$s" title="%2$s">%3$s</a>', |
|
183 get_author_posts_url( $authordata->ID, $authordata->user_nicename ), |
|
184 esc_attr( sprintf( __( 'Posts by %s' ), get_the_author() ) ), |
|
185 get_the_author() |
|
186 ); |
|
187 echo apply_filters( 'the_author_posts_link', $link ); |
|
188 } |
|
189 |
|
190 /** |
|
191 * Retrieve the URL to the author page of the author of the current post. |
|
192 * |
|
193 * @since 2.1.0 |
|
194 * @uses $wp_rewrite WP_Rewrite |
|
195 * @return string The URL to the author's page. |
|
196 */ |
|
197 function get_author_posts_url($author_id, $author_nicename = '') { |
|
198 global $wp_rewrite; |
|
199 $auth_ID = (int) $author_id; |
|
200 $link = $wp_rewrite->get_author_permastruct(); |
|
201 |
|
202 if ( empty($link) ) { |
|
203 $file = get_option('home') . '/'; |
|
204 $link = $file . '?author=' . $auth_ID; |
|
205 } else { |
|
206 if ( '' == $author_nicename ) { |
|
207 $user = get_userdata($author_id); |
|
208 if ( !empty($user->user_nicename) ) |
|
209 $author_nicename = $user->user_nicename; |
|
210 } |
|
211 $link = str_replace('%author%', $author_nicename, $link); |
|
212 $link = get_option('home') . trailingslashit($link); |
|
213 } |
|
214 |
|
215 $link = apply_filters('author_link', $link, $author_id, $author_nicename); |
|
216 |
|
217 return $link; |
|
218 } |
|
219 |
|
220 /** |
|
221 * List all the authors of the blog, with several options available. |
|
222 * |
|
223 * <ul> |
|
224 * <li>optioncount (boolean) (false): Show the count in parenthesis next to the |
|
225 * author's name.</li> |
|
226 * <li>exclude_admin (boolean) (true): Exclude the 'admin' user that is |
|
227 * installed bydefault.</li> |
|
228 * <li>show_fullname (boolean) (false): Show their full names.</li> |
|
229 * <li>hide_empty (boolean) (true): Don't show authors without any posts.</li> |
|
230 * <li>feed (string) (''): If isn't empty, show links to author's feeds.</li> |
|
231 * <li>feed_image (string) (''): If isn't empty, use this image to link to |
|
232 * feeds.</li> |
|
233 * <li>echo (boolean) (true): Set to false to return the output, instead of |
|
234 * echoing.</li> |
|
235 * <li>style (string) ('list'): Whether to display list of authors in list form |
|
236 * or as a string.</li> |
|
237 * <li>html (bool) (true): Whether to list the items in html for or plaintext. |
|
238 * </li> |
|
239 * </ul> |
|
240 * |
|
241 * @link http://codex.wordpress.org/Template_Tags/wp_list_authors |
|
242 * @since 1.2.0 |
|
243 * @param array $args The argument array. |
|
244 * @return null|string The output, if echo is set to false. |
|
245 */ |
|
246 function wp_list_authors($args = '') { |
|
247 global $wpdb; |
|
248 |
|
249 $defaults = array( |
|
250 'optioncount' => false, 'exclude_admin' => true, |
|
251 'show_fullname' => false, 'hide_empty' => true, |
|
252 'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true, |
|
253 'style' => 'list', 'html' => true |
|
254 ); |
|
255 |
|
256 $r = wp_parse_args( $args, $defaults ); |
|
257 extract($r, EXTR_SKIP); |
|
258 $return = ''; |
|
259 |
|
260 /** @todo Move select to get_authors(). */ |
|
261 $authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users " . ($exclude_admin ? "WHERE user_login <> 'admin' " : '') . "ORDER BY display_name"); |
|
262 |
|
263 $author_count = array(); |
|
264 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) { |
|
265 $author_count[$row->post_author] = $row->count; |
|
266 } |
|
267 |
|
268 foreach ( (array) $authors as $author ) { |
|
269 |
|
270 $link = ''; |
|
271 |
|
272 $author = get_userdata( $author->ID ); |
|
273 $posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0; |
|
274 $name = $author->display_name; |
|
275 |
|
276 if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') ) |
|
277 $name = "$author->first_name $author->last_name"; |
|
278 |
|
279 if( !$html ) { |
|
280 if ( $posts == 0 ) { |
|
281 if ( ! $hide_empty ) |
|
282 $return .= $name . ', '; |
|
283 } else |
|
284 $return .= $name . ', '; |
|
285 |
|
286 // No need to go further to process HTML. |
|
287 continue; |
|
288 } |
|
289 |
|
290 if ( !($posts == 0 && $hide_empty) && 'list' == $style ) |
|
291 $return .= '<li>'; |
|
292 if ( $posts == 0 ) { |
|
293 if ( ! $hide_empty ) |
|
294 $link = $name; |
|
295 } else { |
|
296 $link = '<a href="' . get_author_posts_url($author->ID, $author->user_nicename) . '" title="' . esc_attr( sprintf(__("Posts by %s"), $author->display_name) ) . '">' . $name . '</a>'; |
|
297 |
|
298 if ( (! empty($feed_image)) || (! empty($feed)) ) { |
|
299 $link .= ' '; |
|
300 if (empty($feed_image)) |
|
301 $link .= '('; |
|
302 $link .= '<a href="' . get_author_feed_link($author->ID) . '"'; |
|
303 |
|
304 if ( !empty($feed) ) { |
|
305 $title = ' title="' . esc_attr($feed) . '"'; |
|
306 $alt = ' alt="' . esc_attr($feed) . '"'; |
|
307 $name = $feed; |
|
308 $link .= $title; |
|
309 } |
|
310 |
|
311 $link .= '>'; |
|
312 |
|
313 if ( !empty($feed_image) ) |
|
314 $link .= "<img src=\"" . esc_url($feed_image) . "\" style=\"border: none;\"$alt$title" . ' />'; |
|
315 else |
|
316 $link .= $name; |
|
317 |
|
318 $link .= '</a>'; |
|
319 |
|
320 if ( empty($feed_image) ) |
|
321 $link .= ')'; |
|
322 } |
|
323 |
|
324 if ( $optioncount ) |
|
325 $link .= ' ('. $posts . ')'; |
|
326 |
|
327 } |
|
328 |
|
329 if ( !($posts == 0 && $hide_empty) && 'list' == $style ) |
|
330 $return .= $link . '</li>'; |
|
331 else if ( ! $hide_empty ) |
|
332 $return .= $link . ', '; |
|
333 } |
|
334 |
|
335 $return = trim($return, ', '); |
|
336 |
|
337 if ( ! $echo ) |
|
338 return $return; |
|
339 echo $return; |
|
340 } |
|
341 |
|
342 ?> |