3 * Taxonomy API: Core category-specific template tags
11 * Retrieve category link URL.
14 * @see get_term_link()
16 * @param int|object $category Category ID or object.
17 * @return string Link on success, empty string if category does not exist.
19 function get_category_link( $category ) {
20 if ( ! is_object( $category ) )
21 $category = (int) $category;
23 $category = get_term_link( $category, 'category' );
25 if ( is_wp_error( $category ) )
32 * Retrieve category parents with separator.
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.
43 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
45 $parent = get_term( $id, 'category' );
46 if ( is_wp_error( $parent ) )
50 $name = $parent->slug;
52 $name = $parent->name;
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 );
60 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
62 $chain .= $name.$separator;
67 * Retrieve post categories.
69 * This tag may be used outside The Loop by passing a post id as the parameter.
71 * Note: This function only returns results from the default "category" taxonomy.
72 * For custom taxonomies use get_the_terms().
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.
79 function get_the_category( $id = false ) {
80 $categories = get_the_terms( $id, 'category' );
81 if ( ! $categories || is_wp_error( $categories ) )
82 $categories = array();
84 $categories = array_values( $categories );
86 foreach ( array_keys( $categories ) as $key ) {
87 _make_cat_compat( $categories[$key] );
91 * Filter the array of categories to return for a post.
94 * @since 4.4.0 Added `$id` parameter.
96 * @param array $categories An array of categories to return for the post.
97 * @param int $id ID of the post.
99 return apply_filters( 'get_the_categories', $categories, $id );
103 * Sort categories by name.
105 * Used by usort() as a callback, should not be used directly. Can actually be
106 * used to sort any term object.
115 function _usort_terms_by_name( $a, $b ) {
116 return strcmp( $a->name, $b->name );
120 * Sort categories by ID.
122 * Used by usort() as a callback, should not be used directly. Can actually be
123 * used to sort any term object.
132 function _usort_terms_by_ID( $a, $b ) {
133 if ( $a->term_id > $b->term_id )
135 elseif ( $a->term_id < $b->term_id )
142 * Retrieve category name based on category ID.
146 * @param int $cat_ID Category ID.
147 * @return string|WP_Error Category name on success, WP_Error on failure.
149 function get_the_category_by_ID( $cat_ID ) {
150 $cat_ID = (int) $cat_ID;
151 $category = get_term( $cat_ID, 'category' );
153 if ( is_wp_error( $category ) )
156 return ( $category ) ? $category->name : '';
160 * Retrieve category list in either HTML list or custom format.
164 * @global WP_Rewrite $wp_rewrite
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.
171 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
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 );
179 * Filter the categories before building the category list.
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.
187 $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id );
189 if ( empty( $categories ) ) {
190 /** This filter is documented in wp-includes/category-template.php */
191 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
194 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
197 if ( '' == $separator ) {
198 $thelist .= '<ul class="post-categories">';
199 foreach ( $categories as $category ) {
200 $thelist .= "\n\t<li>";
201 switch ( strtolower( $parents ) ) {
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>';
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>';
215 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
221 foreach ( $categories as $category ) {
223 $thelist .= $separator;
224 switch ( strtolower( $parents ) ) {
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>';
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>";
238 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
245 * Filter the category or list of categories.
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.
254 return apply_filters( 'the_category', $thelist, $separator, $parents );
258 * Check if the current post in within any of the given categories.
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.
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.
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.
275 function in_category( $category, $post = null ) {
276 if ( empty( $category ) )
279 return has_category( $category, $post );
283 * Display the category list for the post.
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.
291 function the_category( $separator = '', $parents='', $post_id = false ) {
292 echo get_the_category_list( $separator, $parents, $post_id );
296 * Retrieve category description.
300 * @param int $category Optional. Category ID. Will use global category ID by default.
301 * @return string Category description, available.
303 function category_description( $category = 0 ) {
304 return term_description( $category, 'category' );
308 * Display or retrieve the HTML dropdown list of categories.
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.
316 * @since 4.2.0 Introduced the `value_field` argument.
318 * @param string|array $args {
319 * Optional. Array or string of arguments to generate a categories drop-down element.
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.
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
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|array $taxonomy Name of the category or categories 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).
355 * @return string HTML content only if 'echo' argument is 0.
357 function wp_dropdown_categories( $args = '' ) {
359 'show_option_all' => '', 'show_option_none' => '',
360 'orderby' => 'id', 'order' => 'ASC',
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',
372 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
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>'
383 $args['taxonomy'] = 'link_category';
386 $r = wp_parse_args( $args, $defaults );
387 $option_none_value = $r['option_none_value'];
389 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
390 $r['pad_counts'] = true;
393 $tab_index = $r['tab_index'];
395 $tab_index_attribute = '';
396 if ( (int) $tab_index > 0 ) {
397 $tab_index_attribute = " tabindex=\"$tab_index\"";
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 );
405 $name = esc_attr( $r['name'] );
406 $class = esc_attr( $r['class'] );
407 $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
409 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
410 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
414 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
417 * Filter a taxonomy drop-down display element.
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
426 * @see wp_dropdown_categories()
428 * @param string $element Taxonomy element to list.
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";
434 if ( ! empty( $categories ) ) {
436 if ( $r['show_option_all'] ) {
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";
444 if ( $r['show_option_none'] ) {
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";
452 if ( $r['hierarchical'] ) {
453 $depth = $r['depth']; // Walk the full depth.
455 $depth = -1; // Flat.
457 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
460 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
461 $output .= "</select>\n";
464 * Filter the taxonomy drop-down output.
468 * @param string $output HTML output.
469 * @param array $r Arguments used to build the drop-down.
471 $output = apply_filters( 'wp_dropdown_cats', $output, $r );
480 * Display or retrieve the HTML list of categories.
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.
486 * @param string|array $args {
487 * Array of optional arguments.
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'.
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.
498 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute.
500 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed
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 * @type string $separator Separator between links. Default '<br />'.
525 * @return false|string HTML content only if 'echo' argument is 0.
527 function wp_list_categories( $args = '' ) {
530 'current_category' => 0,
534 'exclude_tree' => '',
539 'hide_title_if_empty' => false,
540 'hierarchical' => true,
543 'separator' => '<br />',
545 'show_option_all' => '',
546 'show_option_none' => __( 'No categories' ),
548 'taxonomy' => 'category',
549 'title_li' => __( 'Categories' ),
550 'use_desc_for_title' => 1,
553 $r = wp_parse_args( $args, $defaults );
555 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
556 $r['pad_counts'] = true;
558 // Descendants of exclusions should be excluded too.
559 if ( true == $r['hierarchical'] ) {
560 $exclude_tree = array();
562 if ( $r['exclude_tree'] ) {
563 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude_tree'] ) );
566 if ( $r['exclude'] ) {
567 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) );
570 $r['exclude_tree'] = $exclude_tree;
574 if ( ! isset( $r['class'] ) )
575 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
577 if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
581 $show_option_all = $r['show_option_all'];
582 $show_option_none = $r['show_option_none'];
584 $categories = get_categories( $r );
587 if ( $r['title_li'] && 'list' == $r['style'] && ( ! empty( $categories ) || ! $r['hide_title_if_empty'] ) ) {
588 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
590 if ( empty( $categories ) ) {
591 if ( ! empty( $show_option_none ) ) {
592 if ( 'list' == $r['style'] ) {
593 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
595 $output .= $show_option_none;
599 if ( ! empty( $show_option_all ) ) {
603 // For taxonomies that belong only to custom post types, point to a valid archive.
604 $taxonomy_object = get_taxonomy( $r['taxonomy'] );
605 if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) {
606 foreach ( $taxonomy_object->object_type as $object_type ) {
607 $_object_type = get_post_type_object( $object_type );
609 // Grab the first one.
610 if ( ! empty( $_object_type->has_archive ) ) {
611 $posts_page = get_post_type_archive_link( $object_type );
617 // Fallback for the 'All' link is the posts page.
618 if ( ! $posts_page ) {
619 if ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) {
620 $posts_page = get_permalink( get_option( 'page_for_posts' ) );
622 $posts_page = home_url( '/' );
626 $posts_page = esc_url( $posts_page );
627 if ( 'list' == $r['style'] ) {
628 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
630 $output .= "<a href='$posts_page'>$show_option_all</a>";
634 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
635 $current_term_object = get_queried_object();
636 if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
637 $r['current_category'] = get_queried_object_id();
641 if ( $r['hierarchical'] ) {
642 $depth = $r['depth'];
644 $depth = -1; // Flat.
646 $output .= walk_category_tree( $categories, $depth, $r );
649 if ( $r['title_li'] && 'list' == $r['style'] )
650 $output .= '</ul></li>';
653 * Filter the HTML output of a taxonomy list.
657 * @param string $output HTML output.
658 * @param array $args An array of taxonomy-listing arguments.
660 $html = apply_filters( 'wp_list_categories', $output, $args );
672 * The text size is set by the 'smallest' and 'largest' arguments, which will
673 * use the 'unit' argument value for the CSS text size unit. The 'format'
674 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
675 * 'format' argument will separate tags with spaces. The list value for the
676 * 'format' argument will format the tags in a UL HTML list. The array value for
677 * the 'format' argument will return in PHP array type format.
679 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
680 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
682 * The 'number' argument is how many tags to return. By default, the limit will
683 * be to return the top 45 tags in the tag cloud list.
685 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
686 * text for the tooltip of the tag link.
688 * The 'topic_count_text_callback' argument is a function, which given the count
689 * of the posts with that tag returns a text for the tooltip of the tag link.
691 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
692 * passed to edit.php for the popular tags edit links.
694 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
695 * function. Only one should be used, because only one will be used and the
696 * other ignored, if they are both set.
700 * @param array|string|null $args Optional. Override default arguments.
701 * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
702 * Otherwise, this function outputs the tag cloud.
704 function wp_tag_cloud( $args = '' ) {
706 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
707 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
708 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
710 $args = wp_parse_args( $args, $defaults );
712 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
714 if ( empty( $tags ) || is_wp_error( $tags ) )
717 foreach ( $tags as $key => $tag ) {
718 if ( 'edit' == $args['link'] )
719 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
721 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
722 if ( is_wp_error( $link ) )
725 $tags[ $key ]->link = $link;
726 $tags[ $key ]->id = $tag->term_id;
729 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
732 * Filter the tag cloud output.
736 * @param string $return HTML output of the tag cloud.
737 * @param array $args An array of tag cloud arguments.
739 $return = apply_filters( 'wp_tag_cloud', $return, $args );
741 if ( 'array' == $args['format'] || empty($args['echo']) )
748 * Default topic count scaling for tag links
750 * @param int $count number of posts with that tag
751 * @return int scaled count
753 function default_topic_count_scale( $count ) {
754 return round(log10($count + 1) * 100);
758 * Generates a tag cloud (heatmap) from provided data.
760 * The text size is set by the 'smallest' and 'largest' arguments, which will
761 * use the 'unit' argument value for the CSS text size unit. The 'format'
762 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
763 * 'format' argument will separate tags with spaces. The list value for the
764 * 'format' argument will format the tags in a UL HTML list. The array value for
765 * the 'format' argument will return in PHP array type format.
767 * The 'tag_cloud_sort' filter allows you to override the sorting.
768 * Passed to the filter: $tags array and $args array, has to return the $tags array
771 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
772 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
775 * The 'number' argument is how many tags to return. By default, the limit will
776 * be to return the entire tag cloud list.
778 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
779 * text for the tooltip of the tag link.
781 * The 'topic_count_text_callback' argument is a function, which given the count
782 * of the posts with that tag returns a text for the tooltip of the tag link.
784 * @todo Complete functionality.
787 * @param array $tags List of tags.
788 * @param string|array $args Optional, override default arguments.
789 * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
791 function wp_generate_tag_cloud( $tags, $args = '' ) {
793 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
794 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
795 'topic_count_text' => null, 'topic_count_text_callback' => null,
796 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
799 $args = wp_parse_args( $args, $defaults );
801 $return = ( 'array' === $args['format'] ) ? array() : '';
803 if ( empty( $tags ) ) {
807 // Juggle topic count tooltips:
808 if ( isset( $args['topic_count_text'] ) ) {
809 // First look for nooped plural support via topic_count_text.
810 $translate_nooped_plural = $args['topic_count_text'];
811 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
812 // Look for the alternative callback style. Ignore the previous default.
813 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
814 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
816 $translate_nooped_plural = false;
818 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
819 // If no callback exists, look for the old-style single_text and multiple_text arguments.
820 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
822 // This is the default for when no callback, plural, or argument is passed in.
823 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
827 * Filter how the items in a tag cloud are sorted.
831 * @param array $tags Ordered array of terms.
832 * @param array $args An array of tag cloud arguments.
834 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
835 if ( empty( $tags_sorted ) ) {
839 if ( $tags_sorted !== $tags ) {
840 $tags = $tags_sorted;
841 unset( $tags_sorted );
843 if ( 'RAND' === $args['order'] ) {
846 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
847 if ( 'name' === $args['orderby'] ) {
848 uasort( $tags, '_wp_object_name_sort_cb' );
850 uasort( $tags, '_wp_object_count_sort_cb' );
853 if ( 'DESC' === $args['order'] ) {
854 $tags = array_reverse( $tags, true );
859 if ( $args['number'] > 0 )
860 $tags = array_slice( $tags, 0, $args['number'] );
863 $real_counts = array(); // For the alt tag
864 foreach ( (array) $tags as $key => $tag ) {
865 $real_counts[ $key ] = $tag->count;
866 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
869 $min_count = min( $counts );
870 $spread = max( $counts ) - $min_count;
873 $font_spread = $args['largest'] - $args['smallest'];
874 if ( $font_spread < 0 )
876 $font_step = $font_spread / $spread;
878 // Assemble the data that will be used to generate the tag cloud markup.
879 $tags_data = array();
880 foreach ( $tags as $key => $tag ) {
881 $tag_id = isset( $tag->id ) ? $tag->id : $key;
883 $count = $counts[ $key ];
884 $real_count = $real_counts[ $key ];
886 if ( $translate_nooped_plural ) {
887 $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
889 $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
892 $tags_data[] = array(
894 'url' => '#' != $tag->link ? $tag->link : '#',
895 'name' => $tag->name,
897 'slug' => $tag->slug,
898 'real_count' => $real_count,
899 'class' => 'tag-link-' . $tag_id,
900 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step,
905 * Filter the data used to generate the tag cloud.
909 * @param array $tags_data An array of term data for term used to generate the tag cloud.
911 $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
915 // generate the output links array
916 foreach ( $tags_data as $key => $tag_data ) {
917 $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 );
918 $a[] = "<a href='" . esc_url( $tag_data['url'] ) . "' class='" . esc_attr( $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>";
921 switch ( $args['format'] ) {
926 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
927 $return .= join( "</li>\n\t<li>", $a );
928 $return .= "</li>\n</ul>\n";
931 $return = join( $args['separator'], $a );
935 if ( $args['filter'] ) {
937 * Filter the generated output of a tag cloud.
939 * The filter is only evaluated if a true value is passed
940 * to the $filter argument in wp_generate_tag_cloud().
944 * @see wp_generate_tag_cloud()
946 * @param array|string $return String containing the generated HTML tag cloud output
947 * or an array of tag links if the 'format' argument
949 * @param array $tags An array of terms used in the tag cloud.
950 * @param array $args An array of wp_generate_tag_cloud() arguments.
952 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
960 * Serves as a callback for comparing objects based on name.
962 * Used with `uasort()`.
967 * @param object $a The first object to compare.
968 * @param object $b The second object to compare.
969 * @return int Negative number if `$a->name` is less than `$b->name`, zero if they are equal,
970 * or greater than zero if `$a->name` is greater than `$b->name`.
972 function _wp_object_name_sort_cb( $a, $b ) {
973 return strnatcasecmp( $a->name, $b->name );
977 * Serves as a callback for comparing objects based on count.
979 * Used with `uasort()`.
984 * @param object $a The first object to compare.
985 * @param object $b The second object to compare.
986 * @return bool Whether the count value for `$a` is greater than the count value for `$b`.
988 function _wp_object_count_sort_cb( $a, $b ) {
989 return ( $a->count > $b->count );
997 * Retrieve HTML list content for category list.
999 * @uses Walker_Category to create HTML list content.
1001 * @see Walker_Category::walk() for parameters and return description.
1004 function walk_category_tree() {
1005 $args = func_get_args();
1006 // the user's options are the third parameter
1007 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
1008 $walker = new Walker_Category;
1010 $walker = $args[2]['walker'];
1012 return call_user_func_array( array( $walker, 'walk' ), $args );
1016 * Retrieve HTML dropdown (select) content for category list.
1018 * @uses Walker_CategoryDropdown to create HTML dropdown content.
1020 * @see Walker_CategoryDropdown::walk() for parameters and return description.
1023 function walk_category_dropdown_tree() {
1024 $args = func_get_args();
1025 // the user's options are the third parameter
1026 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
1027 $walker = new Walker_CategoryDropdown;
1029 $walker = $args[2]['walker'];
1031 return call_user_func_array( array( $walker, 'walk' ), $args );
1039 * Retrieve the link to the tag.
1042 * @see get_term_link()
1044 * @param int|object $tag Tag ID or object.
1045 * @return string Link on success, empty string if tag does not exist.
1047 function get_tag_link( $tag ) {
1048 if ( ! is_object( $tag ) )
1051 $tag = get_term_link( $tag, 'post_tag' );
1053 if ( is_wp_error( $tag ) )
1060 * Retrieve the tags for a post.
1064 * @param int $id Post ID.
1065 * @return array|false|WP_Error Array of tag objects on success, false on failure.
1067 function get_the_tags( $id = 0 ) {
1070 * Filter the array of tags for the given post.
1074 * @see get_the_terms()
1076 * @param array $terms An array of tags for the given post.
1078 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1082 * Retrieve the tags for a post formatted as a string.
1086 * @param string $before Optional. Before tags.
1087 * @param string $sep Optional. Between tags.
1088 * @param string $after Optional. After tags.
1089 * @param int $id Optional. Post ID. Defaults to the current post.
1090 * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
1092 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1095 * Filter the tags list for a given post.
1099 * @param string $tag_list List of tags.
1100 * @param string $before String to use before tags.
1101 * @param string $sep String to use between the tags.
1102 * @param string $after String to use after tags.
1103 * @param int $id Post ID.
1105 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1109 * Retrieve the tags for a post.
1113 * @param string $before Optional. Before list.
1114 * @param string $sep Optional. Separate items using this.
1115 * @param string $after Optional. After list.
1117 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1118 if ( null === $before )
1119 $before = __('Tags: ');
1120 echo get_the_tag_list($before, $sep, $after);
1124 * Retrieve tag description.
1128 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1129 * @return string Tag description, available.
1131 function tag_description( $tag = 0 ) {
1132 return term_description( $tag );
1136 * Retrieve term description.
1140 * @param int $term Optional. Term ID. Will use global term ID by default.
1141 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1142 * @return string Term description, available.
1144 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1145 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1146 $term = get_queried_object();
1148 $taxonomy = $term->taxonomy;
1149 $term = $term->term_id;
1152 $description = get_term_field( 'description', $term, $taxonomy );
1153 return is_wp_error( $description ) ? '' : $description;
1157 * Retrieve the terms of the taxonomy that are attached to the post.
1161 * @param int|object $post Post ID or object.
1162 * @param string $taxonomy Taxonomy name.
1163 * @return array|false|WP_Error Array of term objects on success, false if there are no terms
1164 * or the post does not exist, WP_Error on failure.
1166 function get_the_terms( $post, $taxonomy ) {
1167 if ( ! $post = get_post( $post ) )
1170 $terms = get_object_term_cache( $post->ID, $taxonomy );
1171 if ( false === $terms ) {
1172 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1173 if ( ! is_wp_error( $terms ) ) {
1174 $to_cache = array();
1175 foreach ( $terms as $key => $term ) {
1176 $to_cache[ $key ] = $term->data;
1178 wp_cache_add( $post->ID, $to_cache, $taxonomy . '_relationships' );
1182 if ( ! is_wp_error( $terms ) ) {
1183 $terms = array_map( 'get_term', $terms );
1187 * Filter the list of terms attached to the given post.
1191 * @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
1192 * @param int $post_id Post ID.
1193 * @param string $taxonomy Name of the taxonomy.
1195 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1197 if ( empty( $terms ) )
1204 * Retrieve a post's terms as a list with specified format.
1208 * @param int $id Post ID.
1209 * @param string $taxonomy Taxonomy name.
1210 * @param string $before Optional. Before list.
1211 * @param string $sep Optional. Separate items using this.
1212 * @param string $after Optional. After list.
1213 * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
1215 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1216 $terms = get_the_terms( $id, $taxonomy );
1218 if ( is_wp_error( $terms ) )
1221 if ( empty( $terms ) )
1226 foreach ( $terms as $term ) {
1227 $link = get_term_link( $term, $taxonomy );
1228 if ( is_wp_error( $link ) ) {
1231 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1235 * Filter the term links for a given taxonomy.
1237 * The dynamic portion of the filter name, `$taxonomy`, refers
1238 * to the taxonomy slug.
1242 * @param array $links An array of term links.
1244 $term_links = apply_filters( "term_links-$taxonomy", $links );
1246 return $before . join( $sep, $term_links ) . $after;
1250 * Display the terms in a list.
1254 * @param int $id Post ID.
1255 * @param string $taxonomy Taxonomy name.
1256 * @param string $before Optional. Before list.
1257 * @param string $sep Optional. Separate items using this.
1258 * @param string $after Optional. After list.
1259 * @return false|void False on WordPress error.
1261 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1262 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1264 if ( is_wp_error( $term_list ) )
1268 * Filter the list of terms to display.
1272 * @param array $term_list List of terms to display.
1273 * @param string $taxonomy The taxonomy name.
1274 * @param string $before String to use before the terms.
1275 * @param string $sep String to use between the terms.
1276 * @param string $after String to use after the terms.
1278 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
1282 * Check if the current post has any of given category.
1286 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1287 * @param int|object $post Optional. Post to check instead of the current post.
1288 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1290 function has_category( $category = '', $post = null ) {
1291 return has_term( $category, 'category', $post );
1295 * Check if the current post has any of given tags.
1297 * The given tags are checked against the post's tags' term_ids, names and slugs.
1298 * Tags given as integers will only be checked against the post's tags' term_ids.
1299 * If no tags are given, determines if post has any tags.
1301 * 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)
1302 * Prior to v2.7, this function could only be used in the WordPress Loop.
1303 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1307 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1308 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1309 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1311 function has_tag( $tag = '', $post = null ) {
1312 return has_term( $tag, 'post_tag', $post );
1316 * Check if the current post has any of given terms.
1318 * The given terms are checked against the post's terms' term_ids, names and slugs.
1319 * Terms given as integers will only be checked against the post's terms' term_ids.
1320 * If no terms are given, determines if post has any terms.
1324 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1325 * @param string $taxonomy Taxonomy name
1326 * @param int|object $post Optional. Post to check instead of the current post.
1327 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1329 function has_term( $term = '', $taxonomy = '', $post = null ) {
1330 $post = get_post($post);
1335 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1336 if ( is_wp_error( $r ) )