]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/category-template.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-includes / category-template.php
1 <?php
2 /**
3  * Taxonomy API: Core category-specific template tags
4  *
5  * @package WordPress
6  * @subpackage Template
7  * @since 1.2.0
8  */
9
10 /**
11  * Retrieve category link URL.
12  *
13  * @since 1.0.0
14  * @see get_term_link()
15  *
16  * @param int|object $category Category ID or object.
17  * @return string Link on success, empty string if category does not exist.
18  */
19 function get_category_link( $category ) {
20         if ( ! is_object( $category ) )
21                 $category = (int) $category;
22
23         $category = get_term_link( $category, 'category' );
24
25         if ( is_wp_error( $category ) )
26                 return '';
27
28         return $category;
29 }
30
31 /**
32  * Retrieve category parents with separator.
33  *
34  * @since 1.2.0
35  *
36  * @param int $id Category ID.
37  * @param bool $link Optional, default is false. Whether to format with link.
38  * @param string $separator Optional, default is '/'. How to separate categories.
39  * @param bool $nicename Optional, default is false. Whether to use nice name for display.
40  * @param array $visited Optional. Already linked to categories to prevent duplicates.
41  * @return string|WP_Error A list of category parents on success, WP_Error on failure.
42  */
43 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
44         $chain = '';
45         $parent = get_term( $id, 'category' );
46         if ( is_wp_error( $parent ) )
47                 return $parent;
48
49         if ( $nicename )
50                 $name = $parent->slug;
51         else
52                 $name = $parent->name;
53
54         if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
55                 $visited[] = $parent->parent;
56                 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
57         }
58
59         if ( $link )
60                 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
61         else
62                 $chain .= $name.$separator;
63         return $chain;
64 }
65
66 /**
67  * Retrieve post categories.
68  *
69  * This tag may be used outside The Loop by passing a post id as the parameter.
70  *
71  * Note: This function only returns results from the default "category" taxonomy.
72  * For custom taxonomies use get_the_terms().
73  *
74  * @since 0.71
75  *
76  * @param int $id Optional, default to current post ID. The post ID.
77  * @return array Array of objects, one for each category assigned to the post.
78  */
79 function get_the_category( $id = false ) {
80         $categories = get_the_terms( $id, 'category' );
81         if ( ! $categories || is_wp_error( $categories ) )
82                 $categories = array();
83
84         $categories = array_values( $categories );
85
86         foreach ( array_keys( $categories ) as $key ) {
87                 _make_cat_compat( $categories[$key] );
88         }
89
90         /**
91          * Filter the array of categories to return for a post.
92          *
93          * @since 3.1.0
94          * @since 4.4.0 Added `$id` parameter.
95          *
96          * @param array $categories An array of categories to return for the post.
97          * @param int   $id         ID of the post.
98          */
99         return apply_filters( 'get_the_categories', $categories, $id );
100 }
101
102 /**
103  * Sort categories by name.
104  *
105  * Used by usort() as a callback, should not be used directly. Can actually be
106  * used to sort any term object.
107  *
108  * @since 2.3.0
109  * @access private
110  *
111  * @param object $a
112  * @param object $b
113  * @return int
114  */
115 function _usort_terms_by_name( $a, $b ) {
116         return strcmp( $a->name, $b->name );
117 }
118
119 /**
120  * Sort categories by ID.
121  *
122  * Used by usort() as a callback, should not be used directly. Can actually be
123  * used to sort any term object.
124  *
125  * @since 2.3.0
126  * @access private
127  *
128  * @param object $a
129  * @param object $b
130  * @return int
131  */
132 function _usort_terms_by_ID( $a, $b ) {
133         if ( $a->term_id > $b->term_id )
134                 return 1;
135         elseif ( $a->term_id < $b->term_id )
136                 return -1;
137         else
138                 return 0;
139 }
140
141 /**
142  * Retrieve category name based on category ID.
143  *
144  * @since 0.71
145  *
146  * @param int $cat_ID Category ID.
147  * @return string|WP_Error Category name on success, WP_Error on failure.
148  */
149 function get_the_category_by_ID( $cat_ID ) {
150         $cat_ID = (int) $cat_ID;
151         $category = get_term( $cat_ID, 'category' );
152
153         if ( is_wp_error( $category ) )
154                 return $category;
155
156         return ( $category ) ? $category->name : '';
157 }
158
159 /**
160  * Retrieve category list in either HTML list or custom format.
161  *
162  * @since 1.5.1
163  *
164  * @global WP_Rewrite $wp_rewrite
165  *
166  * @param string $separator Optional, default is empty string. Separator for between the categories.
167  * @param string $parents Optional. How to display the parents.
168  * @param int $post_id Optional. Post ID to retrieve categories.
169  * @return string
170  */
171 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
172         global $wp_rewrite;
173         if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
174                 /** This filter is documented in wp-includes/category-template.php */
175                 return apply_filters( 'the_category', '', $separator, $parents );
176         }
177
178         /**
179          * Filter the categories before building the category list.
180          *
181          * @since 4.4.0
182          *
183          * @param array    $categories An array of the post's categories.
184          * @param int|bool $post_id    ID of the post we're retrieving categories for. When `false`, we assume the
185          *                             current post in the loop.
186          */
187         $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id );
188
189         if ( empty( $categories ) ) {
190                 /** This filter is documented in wp-includes/category-template.php */
191                 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
192         }
193
194         $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
195
196         $thelist = '';
197         if ( '' == $separator ) {
198                 $thelist .= '<ul class="post-categories">';
199                 foreach ( $categories as $category ) {
200                         $thelist .= "\n\t<li>";
201                         switch ( strtolower( $parents ) ) {
202                                 case 'multiple':
203                                         if ( $category->parent )
204                                                 $thelist .= get_category_parents( $category->parent, true, $separator );
205                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
206                                         break;
207                                 case 'single':
208                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '"  ' . $rel . '>';
209                                         if ( $category->parent )
210                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
211                                         $thelist .= $category->name.'</a></li>';
212                                         break;
213                                 case '':
214                                 default:
215                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
216                         }
217                 }
218                 $thelist .= '</ul>';
219         } else {
220                 $i = 0;
221                 foreach ( $categories as $category ) {
222                         if ( 0 < $i )
223                                 $thelist .= $separator;
224                         switch ( strtolower( $parents ) ) {
225                                 case 'multiple':
226                                         if ( $category->parent )
227                                                 $thelist .= get_category_parents( $category->parent, true, $separator );
228                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
229                                         break;
230                                 case 'single':
231                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
232                                         if ( $category->parent )
233                                                 $thelist .= get_category_parents( $category->parent, false, $separator );
234                                         $thelist .= "$category->name</a>";
235                                         break;
236                                 case '':
237                                 default:
238                                         $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
239                         }
240                         ++$i;
241                 }
242         }
243
244         /**
245          * Filter the category or list of categories.
246          *
247          * @since 1.2.0
248          *
249          * @param array  $thelist   List of categories for the current post.
250          * @param string $separator Separator used between the categories.
251          * @param string $parents   How to display the category parents. Accepts 'multiple',
252          *                          'single', or empty.
253          */
254         return apply_filters( 'the_category', $thelist, $separator, $parents );
255 }
256
257 /**
258  * Check if the current post in within any of the given categories.
259  *
260  * The given categories are checked against the post's categories' term_ids, names and slugs.
261  * Categories given as integers will only be checked against the post's categories' term_ids.
262  *
263  * Prior to v2.5 of WordPress, category names were not supported.
264  * Prior to v2.7, category slugs were not supported.
265  * Prior to v2.7, only one category could be compared: in_category( $single_category ).
266  * Prior to v2.7, this function could only be used in the WordPress Loop.
267  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
268  *
269  * @since 1.2.0
270  *
271  * @param int|string|array $category Category ID, name or slug, or array of said.
272  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
273  * @return bool True if the current post is in any of the given categories.
274  */
275 function in_category( $category, $post = null ) {
276         if ( empty( $category ) )
277                 return false;
278
279         return has_category( $category, $post );
280 }
281
282 /**
283  * Display the category list for the post.
284  *
285  * @since 0.71
286  *
287  * @param string $separator Optional, default is empty string. Separator for between the categories.
288  * @param string $parents Optional. How to display the parents.
289  * @param int $post_id Optional. Post ID to retrieve categories.
290  */
291 function the_category( $separator = '', $parents='', $post_id = false ) {
292         echo get_the_category_list( $separator, $parents, $post_id );
293 }
294
295 /**
296  * Retrieve category description.
297  *
298  * @since 1.0.0
299  *
300  * @param int $category Optional. Category ID. Will use global category ID by default.
301  * @return string Category description, available.
302  */
303 function category_description( $category = 0 ) {
304         return term_description( $category, 'category' );
305 }
306
307 /**
308  * Display or retrieve the HTML dropdown list of categories.
309  *
310  * The 'hierarchical' argument, which is disabled by default, will override the
311  * depth argument, unless it is true. When the argument is false, it will
312  * display all of the categories. When it is enabled it will use the value in
313  * the 'depth' argument.
314  *
315  * @since 2.1.0
316  * @since 4.2.0 Introduced the `value_field` argument.
317  *
318  * @param string|array $args {
319  *     Optional. Array or string of arguments to generate a categories drop-down element.
320  *
321  *     @type string       $show_option_all   Text to display for showing all categories. Default empty.
322  *     @type string       $show_option_none  Text to display for showing no categories. Default empty.
323  *     @type string       $option_none_value Value to use when no category is selected. Default empty.
324  *     @type string       $orderby           Which column to use for ordering categories. See get_terms() for a list
325  *                                           of accepted values. Default 'id' (term_id).
326  *     @type string       $order             Whether to order terms in ascending or descending order. Accepts 'ASC'
327  *                                           or 'DESC'. Default 'ASC'.
328  *     @type bool         $pad_counts        See get_terms() for an argument description. Default false.
329  *     @type bool|int     $show_count        Whether to include post counts. Accepts 0, 1, or their bool equivalents.
330  *                                           Default 0.
331  *     @type bool|int     $hide_empty        Whether to hide categories that don't have any posts. Accepts 0, 1, or
332  *                                           their bool equivalents. Default 1.
333  *     @type int          $child_of          Term ID to retrieve child terms of. See get_terms(). Default 0.
334  *     @type array|string $exclude           Array or comma/space-separated string of term ids to exclude.
335  *                                           If `$include` is non-empty, `$exclude` is ignored. Default empty array.
336  *     @type bool|int     $echo              Whether to echo or return the generated markup. Accepts 0, 1, or their
337  *                                           bool equivalents. Default 1.
338  *     @type bool|int     $hierarchical      Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool
339  *                                           equivalents. Default 0.
340  *     @type int          $depth             Maximum depth. Default 0.
341  *     @type int          $tab_index         Tab index for the select element. Default 0 (no tabindex).
342  *     @type string       $name              Value for the 'name' attribute of the select element. Default 'cat'.
343  *     @type string       $id                Value for the 'id' attribute of the select element. Defaults to the value
344  *                                           of `$name`.
345  *     @type string       $class             Value for the 'class' attribute of the select element. Default 'postform'.
346  *     @type int|string   $selected          Value of the option that should be selected. Default 0.
347  *     @type string       $value_field       Term field that should be used to populate the 'value' attribute
348  *                                           of the option elements. Accepts any valid term field: 'term_id', 'name',
349  *                                           'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
350  *                                           'parent', 'count'. Default 'term_id'.
351  *     @type string       $taxonomy          Name of the category to retrieve. Default 'category'.
352  *     @type bool         $hide_if_empty     True to skip generating markup if no categories are found.
353  *                                           Default false (create select element even if no categories are found).
354  * }
355  * @return string HTML content only if 'echo' argument is 0.
356  */
357 function wp_dropdown_categories( $args = '' ) {
358         $defaults = array(
359                 'show_option_all' => '', 'show_option_none' => '',
360                 'orderby' => 'id', 'order' => 'ASC',
361                 'show_count' => 0,
362                 'hide_empty' => 1, 'child_of' => 0,
363                 'exclude' => '', 'echo' => 1,
364                 'selected' => 0, 'hierarchical' => 0,
365                 'name' => 'cat', 'id' => '',
366                 'class' => 'postform', 'depth' => 0,
367                 'tab_index' => 0, 'taxonomy' => 'category',
368                 'hide_if_empty' => false, 'option_none_value' => -1,
369                 'value_field' => 'term_id',
370         );
371
372         $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
373
374         // Back compat.
375         if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
376                 /* translators: 1: "type => link", 2: "taxonomy => link_category" alternative */
377                 _deprecated_argument( __FUNCTION__, '3.0',
378                         sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
379                                 '<code>type => link</code>',
380                                 '<code>taxonomy => link_category</code>'
381                         )
382                 );
383                 $args['taxonomy'] = 'link_category';
384         }
385
386         $r = wp_parse_args( $args, $defaults );
387         $option_none_value = $r['option_none_value'];
388
389         if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
390                 $r['pad_counts'] = true;
391         }
392
393         $tab_index = $r['tab_index'];
394
395         $tab_index_attribute = '';
396         if ( (int) $tab_index > 0 ) {
397                 $tab_index_attribute = " tabindex=\"$tab_index\"";
398         }
399
400         // Avoid clashes with the 'name' param of get_terms().
401         $get_terms_args = $r;
402         unset( $get_terms_args['name'] );
403         $categories = get_terms( $r['taxonomy'], $get_terms_args );
404
405         $name = esc_attr( $r['name'] );
406         $class = esc_attr( $r['class'] );
407         $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
408
409         if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
410                 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
411         } else {
412                 $output = '';
413         }
414         if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
415
416                 /**
417                  * Filter a taxonomy drop-down display element.
418                  *
419                  * A variety of taxonomy drop-down display elements can be modified
420                  * just prior to display via this filter. Filterable arguments include
421                  * 'show_option_none', 'show_option_all', and various forms of the
422                  * term name.
423                  *
424                  * @since 1.2.0
425                  *
426                  * @see wp_dropdown_categories()
427                  *
428                  * @param string $element Taxonomy element to list.
429                  */
430                 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
431                 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
432         }
433
434         if ( ! empty( $categories ) ) {
435
436                 if ( $r['show_option_all'] ) {
437
438                         /** This filter is documented in wp-includes/category-template.php */
439                         $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] );
440                         $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
441                         $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
442                 }
443
444                 if ( $r['show_option_none'] ) {
445
446                         /** This filter is documented in wp-includes/category-template.php */
447                         $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
448                         $selected = selected( $option_none_value, $r['selected'], false );
449                         $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
450                 }
451
452                 if ( $r['hierarchical'] ) {
453                         $depth = $r['depth'];  // Walk the full depth.
454                 } else {
455                         $depth = -1; // Flat.
456                 }
457                 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
458         }
459
460         if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
461                 $output .= "</select>\n";
462         }
463         /**
464          * Filter the taxonomy drop-down output.
465          *
466          * @since 2.1.0
467          *
468          * @param string $output HTML output.
469          * @param array  $r      Arguments used to build the drop-down.
470          */
471         $output = apply_filters( 'wp_dropdown_cats', $output, $r );
472
473         if ( $r['echo'] ) {
474                 echo $output;
475         }
476         return $output;
477 }
478
479 /**
480  * Display or retrieve the HTML list of categories.
481  *
482  * @since 2.1.0
483  * @since 4.4.0 Introduced the `hide_title_if_empty` and `separator` arguments. The `current_category` argument was modified to
484  *              optionally accept an array of values.
485  *
486  * @param string|array $args {
487  *     Array of optional arguments.
488  *
489  *     @type string       $show_option_all       Text to display for showing all categories. Default empty string.
490  *     @type string       $show_option_none      Text to display for the 'no categories' option.
491  *                                               Default 'No categories'.
492  *     @type string       $orderby               The column to use for ordering categories. Default 'ID'.
493  *     @type string       $order                 Which direction to order categories. Accepts 'ASC' or 'DESC'.
494  *                                               Default 'ASC'.
495  *     @type bool|int     $show_count            Whether to show how many posts are in the category. Default 0.
496  *     @type bool|int     $hide_empty            Whether to hide categories that don't have any posts attached to them.
497  *                                               Default 1.
498  *     @type bool|int     $use_desc_for_title    Whether to use the category description as the title attribute.
499  *                                               Default 1.
500  *     @type string       $feed                  Text to use for the feed link. Default 'Feed for all posts filed
501  *                                               under [cat name]'.
502  *     @type string       $feed_type             Feed type. Used to build feed link. See {@link get_term_feed_link()}.
503  *                                               Default empty string (default feed).
504  *     @type string       $feed_image            URL of an image to use for the feed link. Default empty string.
505  *     @type int          $child_of              Term ID to retrieve child terms of. See {@link get_terms()}. Default 0.
506  *     @type array|string $exclude               Array or comma/space-separated string of term IDs to exclude.
507  *                                               If `$hierarchical` is true, descendants of `$exclude` terms will also
508  *                                               be excluded; see `$exclude_tree`. See {@link get_terms()}.
509  *                                               Default empty string.
510  *     @type array|string $exclude_tree          Array or comma/space-separated string of term IDs to exclude, along
511  *                                               with their descendants. See {@link get_terms()}. Default empty string.
512  *     @type bool|int     $echo                  True to echo markup, false to return it. Default 1.
513  *     @type int|array    $current_category      ID of category, or array of IDs of categories, that should get the
514  *                                               'current-cat' class. Default 0.
515  *     @type bool         $hierarchical          Whether to include terms that have non-empty descendants.
516  *                                               See {@link get_terms()}. Default true.
517  *     @type string       $title_li              Text to use for the list title `<li>` element. Pass an empty string
518  *                                               to disable. Default 'Categories'.
519  *     @type bool         $hide_title_if_empty   Whether to hide the `$title_li` element if there are no terms in
520  *                                               the list. Default false (title will always be shown).
521  *     @type int          $depth                 Category depth. Used for tab indentation. Default 0.
522  *     @type string       $taxonomy              Taxonomy name. Default 'category'.
523  * }
524  * @return false|string HTML content only if 'echo' argument is 0.
525  */
526 function wp_list_categories( $args = '' ) {
527         $defaults = array(
528                 'show_option_all' => '', 'show_option_none' => __('No categories'),
529                 'orderby' => 'name', 'order' => 'ASC',
530                 'style' => 'list',
531                 'show_count' => 0, 'hide_empty' => 1,
532                 'use_desc_for_title' => 1, 'child_of' => 0,
533                 'feed' => '', 'feed_type' => '',
534                 'feed_image' => '', 'exclude' => '',
535                 'exclude_tree' => '', 'current_category' => 0,
536                 'hierarchical' => true, 'title_li' => __( 'Categories' ),
537                 'hide_title_if_empty' => false,
538                 'echo' => 1, 'depth' => 0,
539                 'separator' => '<br />',
540                 'taxonomy' => 'category'
541         );
542
543         $r = wp_parse_args( $args, $defaults );
544
545         if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
546                 $r['pad_counts'] = true;
547
548         // Descendants of exclusions should be excluded too.
549         if ( true == $r['hierarchical'] ) {
550                 $exclude_tree = array();
551
552                 if ( $r['exclude_tree'] ) {
553                         $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) );
554                 }
555
556                 if ( $r['exclude'] ) {
557                         $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) );
558                 }
559
560                 $r['exclude_tree'] = $exclude_tree;
561                 $r['exclude'] = '';
562         }
563
564         if ( ! isset( $r['class'] ) )
565                 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
566
567         if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
568                 return false;
569         }
570
571         $show_option_all = $r['show_option_all'];
572         $show_option_none = $r['show_option_none'];
573
574         $categories = get_categories( $r );
575
576         $output = '';
577         if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_empty'] ) ) {
578                 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
579         }
580         if ( empty( $categories ) ) {
581                 if ( ! empty( $show_option_none ) ) {
582                         if ( 'list' == $r['style'] ) {
583                                 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
584                         } else {
585                                 $output .= $show_option_none;
586                         }
587                 }
588         } else {
589                 if ( ! empty( $show_option_all ) ) {
590
591                         $posts_page = '';
592
593                         // For taxonomies that belong only to custom post types, point to a valid archive.
594                         $taxonomy_object = get_taxonomy( $r['taxonomy'] );
595                         if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) {
596                                 foreach ( $taxonomy_object->object_type as $object_type ) {
597                                         $_object_type = get_post_type_object( $object_type );
598
599                                         // Grab the first one.
600                                         if ( ! empty( $_object_type->has_archive ) ) {
601                                                 $posts_page = get_post_type_archive_link( $object_type );
602                                                 break;
603                                         }
604                                 }
605                         }
606
607                         // Fallback for the 'All' link is the posts page.
608                         if ( ! $posts_page ) {
609                                 if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) {
610                                         $posts_page = get_permalink( get_option( 'page_for_posts' ) );
611                                 } else {
612                                         $posts_page = home_url( '/' );
613                                 }
614                         }
615
616                         $posts_page = esc_url( $posts_page );
617                         if ( 'list' == $r['style'] ) {
618                                 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
619                         } else {
620                                 $output .= "<a href='$posts_page'>$show_option_all</a>";
621                         }
622                 }
623
624                 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
625                         $current_term_object = get_queried_object();
626                         if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
627                                 $r['current_category'] = get_queried_object_id();
628                         }
629                 }
630
631                 if ( $r['hierarchical'] ) {
632                         $depth = $r['depth'];
633                 } else {
634                         $depth = -1; // Flat.
635                 }
636                 $output .= walk_category_tree( $categories, $depth, $r );
637         }
638
639         if ( $r['title_li'] && 'list' == $r['style'] )
640                 $output .= '</ul></li>';
641
642         /**
643          * Filter the HTML output of a taxonomy list.
644          *
645          * @since 2.1.0
646          *
647          * @param string $output HTML output.
648          * @param array  $args   An array of taxonomy-listing arguments.
649          */
650         $html = apply_filters( 'wp_list_categories', $output, $args );
651
652         if ( $r['echo'] ) {
653                 echo $html;
654         } else {
655                 return $html;
656         }
657 }
658
659 /**
660  * Display tag cloud.
661  *
662  * The text size is set by the 'smallest' and 'largest' arguments, which will
663  * use the 'unit' argument value for the CSS text size unit. The 'format'
664  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
665  * 'format' argument will separate tags with spaces. The list value for the
666  * 'format' argument will format the tags in a UL HTML list. The array value for
667  * the 'format' argument will return in PHP array type format.
668  *
669  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
670  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
671  *
672  * The 'number' argument is how many tags to return. By default, the limit will
673  * be to return the top 45 tags in the tag cloud list.
674  *
675  * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
676  * text for the tooltip of the tag link.
677  *
678  * The 'topic_count_text_callback' argument is a function, which given the count
679  * of the posts with that tag returns a text for the tooltip of the tag link.
680  *
681  * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
682  * passed to edit.php for the popular tags edit links.
683  *
684  * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
685  * function. Only one should be used, because only one will be used and the
686  * other ignored, if they are both set.
687  *
688  * @since 2.3.0
689  *
690  * @param array|string|null $args Optional. Override default arguments.
691  * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
692  *                    Otherwise, this function outputs the tag cloud.
693  */
694 function wp_tag_cloud( $args = '' ) {
695         $defaults = array(
696                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
697                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
698                 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
699         );
700         $args = wp_parse_args( $args, $defaults );
701
702         $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
703
704         if ( empty( $tags ) || is_wp_error( $tags ) )
705                 return;
706
707         foreach ( $tags as $key => $tag ) {
708                 if ( 'edit' == $args['link'] )
709                         $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
710                 else
711                         $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
712                 if ( is_wp_error( $link ) )
713                         return;
714
715                 $tags[ $key ]->link = $link;
716                 $tags[ $key ]->id = $tag->term_id;
717         }
718
719         $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
720
721         /**
722          * Filter the tag cloud output.
723          *
724          * @since 2.3.0
725          *
726          * @param string $return HTML output of the tag cloud.
727          * @param array  $args   An array of tag cloud arguments.
728          */
729         $return = apply_filters( 'wp_tag_cloud', $return, $args );
730
731         if ( 'array' == $args['format'] || empty($args['echo']) )
732                 return $return;
733
734         echo $return;
735 }
736
737 /**
738  * Default topic count scaling for tag links
739  *
740  * @param int $count number of posts with that tag
741  * @return int scaled count
742  */
743 function default_topic_count_scale( $count ) {
744         return round(log10($count + 1) * 100);
745 }
746
747 /**
748  * Generates a tag cloud (heatmap) from provided data.
749  *
750  * The text size is set by the 'smallest' and 'largest' arguments, which will
751  * use the 'unit' argument value for the CSS text size unit. The 'format'
752  * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
753  * 'format' argument will separate tags with spaces. The list value for the
754  * 'format' argument will format the tags in a UL HTML list. The array value for
755  * the 'format' argument will return in PHP array type format.
756  *
757  * The 'tag_cloud_sort' filter allows you to override the sorting.
758  * Passed to the filter: $tags array and $args array, has to return the $tags array
759  * after sorting it.
760  *
761  * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
762  * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
763  * 'RAND'.
764  *
765  * The 'number' argument is how many tags to return. By default, the limit will
766  * be to return the entire tag cloud list.
767  *
768  * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
769  * text for the tooltip of the tag link.
770  *
771  * The 'topic_count_text_callback' argument is a function, which given the count
772  * of the posts with that tag returns a text for the tooltip of the tag link.
773  *
774  * @todo Complete functionality.
775  * @since 2.3.0
776  *
777  * @param array $tags List of tags.
778  * @param string|array $args Optional, override default arguments.
779  * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
780  */
781 function wp_generate_tag_cloud( $tags, $args = '' ) {
782         $defaults = array(
783                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
784                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
785                 'topic_count_text' => null, 'topic_count_text_callback' => null,
786                 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
787         );
788
789         $args = wp_parse_args( $args, $defaults );
790
791         $return = ( 'array' === $args['format'] ) ? array() : '';
792
793         if ( empty( $tags ) ) {
794                 return $return;
795         }
796
797         // Juggle topic count tooltips:
798         if ( isset( $args['topic_count_text'] ) ) {
799                 // First look for nooped plural support via topic_count_text.
800                 $translate_nooped_plural = $args['topic_count_text'];
801         } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
802                 // Look for the alternative callback style. Ignore the previous default.
803                 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
804                         $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
805                 } else {
806                         $translate_nooped_plural = false;
807                 }
808         } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
809                 // If no callback exists, look for the old-style single_text and multiple_text arguments.
810                 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
811         } else {
812                 // This is the default for when no callback, plural, or argument is passed in.
813                 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
814         }
815
816         /**
817          * Filter how the items in a tag cloud are sorted.
818          *
819          * @since 2.8.0
820          *
821          * @param array $tags Ordered array of terms.
822          * @param array $args An array of tag cloud arguments.
823          */
824         $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
825         if ( empty( $tags_sorted ) ) {
826                 return $return;
827         }
828
829         if ( $tags_sorted !== $tags ) {
830                 $tags = $tags_sorted;
831                 unset( $tags_sorted );
832         } else {
833                 if ( 'RAND' === $args['order'] ) {
834                         shuffle( $tags );
835                 } else {
836                         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
837                         if ( 'name' === $args['orderby'] ) {
838                                 uasort( $tags, '_wp_object_name_sort_cb' );
839                         } else {
840                                 uasort( $tags, '_wp_object_count_sort_cb' );
841                         }
842
843                         if ( 'DESC' === $args['order'] ) {
844                                 $tags = array_reverse( $tags, true );
845                         }
846                 }
847         }
848
849         if ( $args['number'] > 0 )
850                 $tags = array_slice( $tags, 0, $args['number'] );
851
852         $counts = array();
853         $real_counts = array(); // For the alt tag
854         foreach ( (array) $tags as $key => $tag ) {
855                 $real_counts[ $key ] = $tag->count;
856                 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
857         }
858
859         $min_count = min( $counts );
860         $spread = max( $counts ) - $min_count;
861         if ( $spread <= 0 )
862                 $spread = 1;
863         $font_spread = $args['largest'] - $args['smallest'];
864         if ( $font_spread < 0 )
865                 $font_spread = 1;
866         $font_step = $font_spread / $spread;
867
868         // Assemble the data that will be used to generate the tag cloud markup.
869         $tags_data = array();
870         foreach ( $tags as $key => $tag ) {
871                 $tag_id = isset( $tag->id ) ? $tag->id : $key;
872
873                 $count = $counts[ $key ];
874                 $real_count = $real_counts[ $key ];
875
876                 if ( $translate_nooped_plural ) {
877                         $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
878                 } else {
879                         $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
880                 }
881
882                 $tags_data[] = array(
883                         'id'         => $tag_id,
884                         'url'        => '#' != $tag->link ? $tag->link : '#',
885                         'name'       => $tag->name,
886                         'title'      => $title,
887                         'slug'       => $tag->slug,
888                         'real_count' => $real_count,
889                         'class'      => 'tag-link-' . $tag_id,
890                         'font_size'  => $args['smallest'] + ( $count - $min_count ) * $font_step,
891                 );
892         }
893
894         /**
895          * Filter the data used to generate the tag cloud.
896          *
897          * @since 4.3.0
898          *
899          * @param array $tags_data An array of term data for term used to generate the tag cloud.
900          */
901         $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
902
903         $a = array();
904
905         // generate the output links array
906         foreach ( $tags_data as $key => $tag_data ) {
907                 $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "' class='" . esc_attr( $tag_data['class'] ) . "' title='" . esc_attr( $tag_data['title'] ) . "' style='font-size: " . esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ) . ";'>" . esc_html( $tag_data['name'] ) . "</a>";
908         }
909
910         switch ( $args['format'] ) {
911                 case 'array' :
912                         $return =& $a;
913                         break;
914                 case 'list' :
915                         $return = "<ul class='wp-tag-cloud'>\n\t<li>";
916                         $return .= join( "</li>\n\t<li>", $a );
917                         $return .= "</li>\n</ul>\n";
918                         break;
919                 default :
920                         $return = join( $args['separator'], $a );
921                         break;
922         }
923
924         if ( $args['filter'] ) {
925                 /**
926                  * Filter the generated output of a tag cloud.
927                  *
928                  * The filter is only evaluated if a true value is passed
929                  * to the $filter argument in wp_generate_tag_cloud().
930                  *
931                  * @since 2.3.0
932                  *
933                  * @see wp_generate_tag_cloud()
934                  *
935                  * @param array|string $return String containing the generated HTML tag cloud output
936                  *                             or an array of tag links if the 'format' argument
937                  *                             equals 'array'.
938                  * @param array        $tags   An array of terms used in the tag cloud.
939                  * @param array        $args   An array of wp_generate_tag_cloud() arguments.
940                  */
941                 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
942         }
943
944         else
945                 return $return;
946 }
947
948 /**
949  * Callback for comparing objects based on name
950  *
951  * @since 3.1.0
952  * @access private
953  * @return int
954  */
955 function _wp_object_name_sort_cb( $a, $b ) {
956         return strnatcasecmp( $a->name, $b->name );
957 }
958
959 /**
960  * Callback for comparing objects based on count
961  *
962  * @since 3.1.0
963  * @access private
964  * @return bool
965  */
966 function _wp_object_count_sort_cb( $a, $b ) {
967         return ( $a->count > $b->count );
968 }
969
970 //
971 // Helper functions
972 //
973
974 /**
975  * Retrieve HTML list content for category list.
976  *
977  * @uses Walker_Category to create HTML list content.
978  * @since 2.1.0
979  * @see Walker_Category::walk() for parameters and return description.
980  * @return string
981  */
982 function walk_category_tree() {
983         $args = func_get_args();
984         // the user's options are the third parameter
985         if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
986                 $walker = new Walker_Category;
987         } else {
988                 $walker = $args[2]['walker'];
989         }
990         return call_user_func_array( array( $walker, 'walk' ), $args );
991 }
992
993 /**
994  * Retrieve HTML dropdown (select) content for category list.
995  *
996  * @uses Walker_CategoryDropdown to create HTML dropdown content.
997  * @since 2.1.0
998  * @see Walker_CategoryDropdown::walk() for parameters and return description.
999  * @return string
1000  */
1001 function walk_category_dropdown_tree() {
1002         $args = func_get_args();
1003         // the user's options are the third parameter
1004         if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
1005                 $walker = new Walker_CategoryDropdown;
1006         } else {
1007                 $walker = $args[2]['walker'];
1008         }
1009         return call_user_func_array( array( $walker, 'walk' ), $args );
1010 }
1011
1012 //
1013 // Tags
1014 //
1015
1016 /**
1017  * Retrieve the link to the tag.
1018  *
1019  * @since 2.3.0
1020  * @see get_term_link()
1021  *
1022  * @param int|object $tag Tag ID or object.
1023  * @return string Link on success, empty string if tag does not exist.
1024  */
1025 function get_tag_link( $tag ) {
1026         if ( ! is_object( $tag ) )
1027                 $tag = (int) $tag;
1028
1029         $tag = get_term_link( $tag, 'post_tag' );
1030
1031         if ( is_wp_error( $tag ) )
1032                 return '';
1033
1034         return $tag;
1035 }
1036
1037 /**
1038  * Retrieve the tags for a post.
1039  *
1040  * @since 2.3.0
1041  *
1042  * @param int $id Post ID.
1043  * @return array|false|WP_Error Array of tag objects on success, false on failure.
1044  */
1045 function get_the_tags( $id = 0 ) {
1046
1047         /**
1048          * Filter the array of tags for the given post.
1049          *
1050          * @since 2.3.0
1051          *
1052          * @see get_the_terms()
1053          *
1054          * @param array $terms An array of tags for the given post.
1055          */
1056         return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1057 }
1058
1059 /**
1060  * Retrieve the tags for a post formatted as a string.
1061  *
1062  * @since 2.3.0
1063  *
1064  * @param string $before Optional. Before tags.
1065  * @param string $sep Optional. Between tags.
1066  * @param string $after Optional. After tags.
1067  * @param int $id Optional. Post ID. Defaults to the current post.
1068  * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
1069  */
1070 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1071
1072         /**
1073          * Filter the tags list for a given post.
1074          *
1075          * @since 2.3.0
1076          *
1077          * @param string $tag_list List of tags.
1078          * @param string $before   String to use before tags.
1079          * @param string $sep      String to use between the tags.
1080          * @param string $after    String to use after tags.
1081          * @param int    $id       Post ID.
1082          */
1083         return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1084 }
1085
1086 /**
1087  * Retrieve the tags for a post.
1088  *
1089  * @since 2.3.0
1090  *
1091  * @param string $before Optional. Before list.
1092  * @param string $sep Optional. Separate items using this.
1093  * @param string $after Optional. After list.
1094  */
1095 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1096         if ( null === $before )
1097                 $before = __('Tags: ');
1098         echo get_the_tag_list($before, $sep, $after);
1099 }
1100
1101 /**
1102  * Retrieve tag description.
1103  *
1104  * @since 2.8.0
1105  *
1106  * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1107  * @return string Tag description, available.
1108  */
1109 function tag_description( $tag = 0 ) {
1110         return term_description( $tag );
1111 }
1112
1113 /**
1114  * Retrieve term description.
1115  *
1116  * @since 2.8.0
1117  *
1118  * @param int $term Optional. Term ID. Will use global term ID by default.
1119  * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1120  * @return string Term description, available.
1121  */
1122 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1123         if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1124                 $term = get_queried_object();
1125                 if ( $term ) {
1126                         $taxonomy = $term->taxonomy;
1127                         $term = $term->term_id;
1128                 }
1129         }
1130         $description = get_term_field( 'description', $term, $taxonomy );
1131         return is_wp_error( $description ) ? '' : $description;
1132 }
1133
1134 /**
1135  * Retrieve the terms of the taxonomy that are attached to the post.
1136  *
1137  * @since 2.5.0
1138  *
1139  * @param int|object $post Post ID or object.
1140  * @param string $taxonomy Taxonomy name.
1141  * @return array|false|WP_Error Array of term objects on success, false if there are no terms
1142  *                              or the post does not exist, WP_Error on failure.
1143  */
1144 function get_the_terms( $post, $taxonomy ) {
1145         if ( ! $post = get_post( $post ) )
1146                 return false;
1147
1148         $terms = get_object_term_cache( $post->ID, $taxonomy );
1149         if ( false === $terms ) {
1150                 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1151                 if ( ! is_wp_error( $terms ) ) {
1152                         $to_cache = array();
1153                         foreach ( $terms as $key => $term ) {
1154                                 $to_cache[ $key ] = $term->data;
1155                         }
1156                         wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' );
1157                 }
1158         }
1159
1160         if ( ! is_wp_error( $terms ) ) {
1161                 $terms = array_map( 'get_term', $terms );
1162         }
1163
1164         /**
1165          * Filter the list of terms attached to the given post.
1166          *
1167          * @since 3.1.0
1168          *
1169          * @param array|WP_Error $terms    List of attached terms, or WP_Error on failure.
1170          * @param int            $post_id  Post ID.
1171          * @param string         $taxonomy Name of the taxonomy.
1172          */
1173         $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1174
1175         if ( empty( $terms ) )
1176                 return false;
1177
1178         return $terms;
1179 }
1180
1181 /**
1182  * Retrieve a post's terms as a list with specified format.
1183  *
1184  * @since 2.5.0
1185  *
1186  * @param int $id Post ID.
1187  * @param string $taxonomy Taxonomy name.
1188  * @param string $before Optional. Before list.
1189  * @param string $sep Optional. Separate items using this.
1190  * @param string $after Optional. After list.
1191  * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
1192  */
1193 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1194         $terms = get_the_terms( $id, $taxonomy );
1195
1196         if ( is_wp_error( $terms ) )
1197                 return $terms;
1198
1199         if ( empty( $terms ) )
1200                 return false;
1201
1202         $links = array();
1203
1204         foreach ( $terms as $term ) {
1205                 $link = get_term_link( $term, $taxonomy );
1206                 if ( is_wp_error( $link ) ) {
1207                         return $link;
1208                 }
1209                 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1210         }
1211
1212         /**
1213          * Filter the term links for a given taxonomy.
1214          *
1215          * The dynamic portion of the filter name, `$taxonomy`, refers
1216          * to the taxonomy slug.
1217          *
1218          * @since 2.5.0
1219          *
1220          * @param array $links An array of term links.
1221          */
1222         $term_links = apply_filters( "term_links-$taxonomy", $links );
1223
1224         return $before . join( $sep, $term_links ) . $after;
1225 }
1226
1227 /**
1228  * Display the terms in a list.
1229  *
1230  * @since 2.5.0
1231  *
1232  * @param int $id Post ID.
1233  * @param string $taxonomy Taxonomy name.
1234  * @param string $before Optional. Before list.
1235  * @param string $sep Optional. Separate items using this.
1236  * @param string $after Optional. After list.
1237  * @return false|void False on WordPress error.
1238  */
1239 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1240         $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1241
1242         if ( is_wp_error( $term_list ) )
1243                 return false;
1244
1245         /**
1246          * Filter the list of terms to display.
1247          *
1248          * @since 2.9.0
1249          *
1250          * @param array  $term_list List of terms to display.
1251          * @param string $taxonomy  The taxonomy name.
1252          * @param string $before    String to use before the terms.
1253          * @param string $sep       String to use between the terms.
1254          * @param string $after     String to use after the terms.
1255          */
1256         echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
1257 }
1258
1259 /**
1260  * Check if the current post has any of given category.
1261  *
1262  * @since 3.1.0
1263  *
1264  * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1265  * @param int|object $post Optional. Post to check instead of the current post.
1266  * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1267  */
1268 function has_category( $category = '', $post = null ) {
1269         return has_term( $category, 'category', $post );
1270 }
1271
1272 /**
1273  * Check if the current post has any of given tags.
1274  *
1275  * The given tags are checked against the post's tags' term_ids, names and slugs.
1276  * Tags given as integers will only be checked against the post's tags' term_ids.
1277  * If no tags are given, determines if post has any tags.
1278  *
1279  * Prior to v2.7 of WordPress, tags given as integers would also be checked against the post's tags' names and slugs (in addition to term_ids)
1280  * Prior to v2.7, this function could only be used in the WordPress Loop.
1281  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1282  *
1283  * @since 2.6.0
1284  *
1285  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1286  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1287  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1288  */
1289 function has_tag( $tag = '', $post = null ) {
1290         return has_term( $tag, 'post_tag', $post );
1291 }
1292
1293 /**
1294  * Check if the current post has any of given terms.
1295  *
1296  * The given terms are checked against the post's terms' term_ids, names and slugs.
1297  * Terms given as integers will only be checked against the post's terms' term_ids.
1298  * If no terms are given, determines if post has any terms.
1299  *
1300  * @since 3.1.0
1301  *
1302  * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1303  * @param string $taxonomy Taxonomy name
1304  * @param int|object $post Optional. Post to check instead of the current post.
1305  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1306  */
1307 function has_term( $term = '', $taxonomy = '', $post = null ) {
1308         $post = get_post($post);
1309
1310         if ( !$post )
1311                 return false;
1312
1313         $r = is_object_in_term( $post->ID, $taxonomy, $term );
1314         if ( is_wp_error( $r ) )
1315                 return false;
1316
1317         return $r;
1318 }