3 * Category Template Tags and API.
10 * Retrieve category link URL.
13 * @see get_term_link()
15 * @param int|object $category Category ID or object.
16 * @return string Link on success, empty string if category does not exist.
18 function get_category_link( $category ) {
19 if ( ! is_object( $category ) )
20 $category = (int) $category;
22 $category = get_term_link( $category, 'category' );
24 if ( is_wp_error( $category ) )
31 * Retrieve category parents with separator.
35 * @param int $id Category ID.
36 * @param bool $link Optional, default is false. Whether to format with link.
37 * @param string $separator Optional, default is '/'. How to separate categories.
38 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
39 * @param array $visited Optional. Already linked to categories to prevent duplicates.
40 * @return string|WP_Error A list of category parents on success, WP_Error on failure.
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
44 $parent = get_term( $id, 'category' );
45 if ( is_wp_error( $parent ) )
49 $name = $parent->slug;
51 $name = $parent->name;
53 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
54 $visited[] = $parent->parent;
55 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
59 $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator;
61 $chain .= $name.$separator;
66 * Retrieve post categories.
70 * @param int $id Optional, default to current post ID. The post ID.
73 function get_the_category( $id = false ) {
74 $categories = get_the_terms( $id, 'category' );
75 if ( ! $categories || is_wp_error( $categories ) )
76 $categories = array();
78 $categories = array_values( $categories );
80 foreach ( array_keys( $categories ) as $key ) {
81 _make_cat_compat( $categories[$key] );
85 * Filter the array of categories to return for a post.
89 * @param array $categories An array of categories to return for the post.
91 return apply_filters( 'get_the_categories', $categories );
95 * Sort categories by name.
97 * Used by usort() as a callback, should not be used directly. Can actually be
98 * used to sort any term object.
107 function _usort_terms_by_name( $a, $b ) {
108 return strcmp( $a->name, $b->name );
112 * Sort categories by ID.
114 * Used by usort() as a callback, should not be used directly. Can actually be
115 * used to sort any term object.
124 function _usort_terms_by_ID( $a, $b ) {
125 if ( $a->term_id > $b->term_id )
127 elseif ( $a->term_id < $b->term_id )
134 * Retrieve category name based on category ID.
138 * @param int $cat_ID Category ID.
139 * @return string|WP_Error Category name on success, WP_Error on failure.
141 function get_the_category_by_ID( $cat_ID ) {
142 $cat_ID = (int) $cat_ID;
143 $category = get_term( $cat_ID, 'category' );
145 if ( is_wp_error( $category ) )
148 return ( $category ) ? $category->name : '';
152 * Retrieve category list in either HTML list or custom format.
156 * @param string $separator Optional, default is empty string. Separator for between the categories.
157 * @param string $parents Optional. How to display the parents.
158 * @param int $post_id Optional. Post ID to retrieve categories.
161 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
163 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
164 /** This filter is documented in wp-includes/category-template.php */
165 return apply_filters( 'the_category', '', $separator, $parents );
168 $categories = get_the_category( $post_id );
169 if ( empty( $categories ) ) {
170 /** This filter is documented in wp-includes/category-template.php */
171 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
174 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
177 if ( '' == $separator ) {
178 $thelist .= '<ul class="post-categories">';
179 foreach ( $categories as $category ) {
180 $thelist .= "\n\t<li>";
181 switch ( strtolower( $parents ) ) {
183 if ( $category->parent )
184 $thelist .= get_category_parents( $category->parent, true, $separator );
185 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
188 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
189 if ( $category->parent )
190 $thelist .= get_category_parents( $category->parent, false, $separator );
191 $thelist .= $category->name.'</a></li>';
195 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
201 foreach ( $categories as $category ) {
203 $thelist .= $separator;
204 switch ( strtolower( $parents ) ) {
206 if ( $category->parent )
207 $thelist .= get_category_parents( $category->parent, true, $separator );
208 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
211 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
212 if ( $category->parent )
213 $thelist .= get_category_parents( $category->parent, false, $separator );
214 $thelist .= "$category->name</a>";
218 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
225 * Filter the category or list of categories.
229 * @param array $thelist List of categories for the current post.
230 * @param string $separator Separator used between the categories.
231 * @param string $parents How to display the category parents. Accepts 'multiple',
232 * 'single', or empty.
234 return apply_filters( 'the_category', $thelist, $separator, $parents );
238 * Check if the current post in within any of the given categories.
240 * The given categories are checked against the post's categories' term_ids, names and slugs.
241 * Categories given as integers will only be checked against the post's categories' term_ids.
243 * Prior to v2.5 of WordPress, category names were not supported.
244 * Prior to v2.7, category slugs were not supported.
245 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
246 * Prior to v2.7, this function could only be used in the WordPress Loop.
247 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
251 * @param int|string|array $category Category ID, name or slug, or array of said.
252 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
253 * @return bool True if the current post is in any of the given categories.
255 function in_category( $category, $post = null ) {
256 if ( empty( $category ) )
259 return has_category( $category, $post );
263 * Display the category list for the post.
267 * @param string $separator Optional, default is empty string. Separator for between the categories.
268 * @param string $parents Optional. How to display the parents.
269 * @param int $post_id Optional. Post ID to retrieve categories.
271 function the_category( $separator = '', $parents='', $post_id = false ) {
272 echo get_the_category_list( $separator, $parents, $post_id );
276 * Retrieve category description.
280 * @param int $category Optional. Category ID. Will use global category ID by default.
281 * @return string Category description, available.
283 function category_description( $category = 0 ) {
284 return term_description( $category, 'category' );
288 * Display or retrieve the HTML dropdown list of categories.
290 * The 'hierarchical' argument, which is disabled by default, will override the
291 * depth argument, unless it is true. When the argument is false, it will
292 * display all of the categories. When it is enabled it will use the value in
293 * the 'depth' argument.
296 * @since 4.2.0 Introduced the `value_field` argument.
298 * @param string|array $args {
299 * Optional. Array or string of arguments to generate a categories drop-down element.
301 * @type string $show_option_all Text to display for showing all categories. Default empty.
302 * @type string $show_option_none Text to display for showing no categories. Default empty.
303 * @type string $option_none_value Value to use when no category is selected. Default empty.
304 * @type string $orderby Which column to use for ordering categories. See get_terms() for a list
305 * of accepted values. Default 'id' (term_id).
306 * @type string $order Whether to order terms in ascending or descending order. Accepts 'ASC'
307 * or 'DESC'. Default 'ASC'.
308 * @type bool $pad_counts See get_terms() for an argument description. Default false.
309 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents.
311 * @type bool|int $hide_empty Whether to hide categories that don't have any posts. Accepts 0, 1, or
312 * their bool equivalents. Default 1.
313 * @type int $child_of Term ID to retrieve child terms of. See get_terms(). Default 0.
314 * @type array|string $exclude Array or comma/space-separated string of term ids to exclude.
315 * If `$include` is non-empty, `$exclude` is ignored. Default empty array.
316 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their
317 * bool equivalents. Default 1.
318 * @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool
319 * equivalents. Default 0.
320 * @type int $depth Maximum depth. Default 0.
321 * @type int $tab_index Tab index for the select element. Default 0 (no tabindex).
322 * @type string $name Value for the 'name' attribute of the select element. Default 'cat'.
323 * @type string $id Value for the 'id' attribute of the select element. Defaults to the value
325 * @type string $class Value for the 'class' attribute of the select element. Default 'postform'.
326 * @type int|string $selected Value of the option that should be selected. Default 0.
327 * @type string $value_field Term field that should be used to populate the 'value' attribute
328 * of the option elements. Accepts any valid term field: 'term_id', 'name',
329 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
330 * 'parent', 'count'. Default 'term_id'.
331 * @type string $taxonomy Name of the category to retrieve. Default 'category'.
332 * @type bool $hide_if_empty True to skip generating markup if no categories are found.
333 * Default false (create select element even if no categories are found).
335 * @return string HTML content only if 'echo' argument is 0.
337 function wp_dropdown_categories( $args = '' ) {
339 'show_option_all' => '', 'show_option_none' => '',
340 'orderby' => 'id', 'order' => 'ASC',
342 'hide_empty' => 1, 'child_of' => 0,
343 'exclude' => '', 'echo' => 1,
344 'selected' => 0, 'hierarchical' => 0,
345 'name' => 'cat', 'id' => '',
346 'class' => 'postform', 'depth' => 0,
347 'tab_index' => 0, 'taxonomy' => 'category',
348 'hide_if_empty' => false, 'option_none_value' => -1,
349 'value_field' => 'term_id',
352 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
355 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
356 _deprecated_argument( __FUNCTION__, '3.0', '' );
357 $args['taxonomy'] = 'link_category';
360 $r = wp_parse_args( $args, $defaults );
361 $option_none_value = $r['option_none_value'];
363 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
364 $r['pad_counts'] = true;
367 $tab_index = $r['tab_index'];
369 $tab_index_attribute = '';
370 if ( (int) $tab_index > 0 ) {
371 $tab_index_attribute = " tabindex=\"$tab_index\"";
374 // Avoid clashes with the 'name' param of get_terms().
375 $get_terms_args = $r;
376 unset( $get_terms_args['name'] );
377 $categories = get_terms( $r['taxonomy'], $get_terms_args );
379 $name = esc_attr( $r['name'] );
380 $class = esc_attr( $r['class'] );
381 $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
383 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
384 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
388 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
391 * Filter a taxonomy drop-down display element.
393 * A variety of taxonomy drop-down display elements can be modified
394 * just prior to display via this filter. Filterable arguments include
395 * 'show_option_none', 'show_option_all', and various forms of the
400 * @see wp_dropdown_categories()
402 * @param string $element Taxonomy element to list.
404 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
405 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
408 if ( ! empty( $categories ) ) {
410 if ( $r['show_option_all'] ) {
412 /** This filter is documented in wp-includes/category-template.php */
413 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] );
414 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
415 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
418 if ( $r['show_option_none'] ) {
420 /** This filter is documented in wp-includes/category-template.php */
421 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
422 $selected = selected( $option_none_value, $r['selected'], false );
423 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
426 if ( $r['hierarchical'] ) {
427 $depth = $r['depth']; // Walk the full depth.
429 $depth = -1; // Flat.
431 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
434 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
435 $output .= "</select>\n";
438 * Filter the taxonomy drop-down output.
442 * @param string $output HTML output.
443 * @param array $r Arguments used to build the drop-down.
445 $output = apply_filters( 'wp_dropdown_cats', $output, $r );
454 * Display or retrieve the HTML list of categories.
456 * The list of arguments is below:
457 * 'show_option_all' (string) - Text to display for showing all categories.
458 * 'orderby' (string) default is 'ID' - What column to use for ordering the
460 * 'order' (string) default is 'ASC' - What direction to order categories.
461 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
463 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
464 * don't have any posts attached to them.
465 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
466 * category description as the title attribute.
467 * 'feed' - See {@link get_categories()}.
468 * 'feed_type' - See {@link get_categories()}.
469 * 'feed_image' - See {@link get_categories()}.
470 * 'child_of' (int) default is 0 - See {@link get_categories()}.
471 * 'exclude' (string) - See {@link get_categories()}.
472 * 'exclude_tree' (string) - See {@link get_categories()}.
473 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
474 * 'current_category' (int) - See {@link get_categories()}.
475 * 'hierarchical' (bool) - See {@link get_categories()}.
476 * 'title_li' (string) - See {@link get_categories()}.
477 * 'depth' (int) - The max depth.
481 * @param string|array $args Optional. Override default arguments.
482 * @return false|null|string HTML content only if 'echo' argument is 0.
484 function wp_list_categories( $args = '' ) {
486 'show_option_all' => '', 'show_option_none' => __('No categories'),
487 'orderby' => 'name', 'order' => 'ASC',
489 'show_count' => 0, 'hide_empty' => 1,
490 'use_desc_for_title' => 1, 'child_of' => 0,
491 'feed' => '', 'feed_type' => '',
492 'feed_image' => '', 'exclude' => '',
493 'exclude_tree' => '', 'current_category' => 0,
494 'hierarchical' => true, 'title_li' => __( 'Categories' ),
495 'echo' => 1, 'depth' => 0,
496 'taxonomy' => 'category'
499 $r = wp_parse_args( $args, $defaults );
501 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
502 $r['pad_counts'] = true;
504 if ( true == $r['hierarchical'] ) {
505 $r['exclude_tree'] = $r['exclude'];
509 if ( ! isset( $r['class'] ) )
510 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
512 if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
516 $show_option_all = $r['show_option_all'];
517 $show_option_none = $r['show_option_none'];
519 $categories = get_categories( $r );
522 if ( $r['title_li'] && 'list' == $r['style'] ) {
523 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
525 if ( empty( $categories ) ) {
526 if ( ! empty( $show_option_none ) ) {
527 if ( 'list' == $r['style'] ) {
528 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
530 $output .= $show_option_none;
534 if ( ! empty( $show_option_all ) ) {
535 $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
536 $posts_page = esc_url( $posts_page );
537 if ( 'list' == $r['style'] ) {
538 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
540 $output .= "<a href='$posts_page'>$show_option_all</a>";
544 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
545 $current_term_object = get_queried_object();
546 if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
547 $r['current_category'] = get_queried_object_id();
551 if ( $r['hierarchical'] ) {
552 $depth = $r['depth'];
554 $depth = -1; // Flat.
556 $output .= walk_category_tree( $categories, $depth, $r );
559 if ( $r['title_li'] && 'list' == $r['style'] )
560 $output .= '</ul></li>';
563 * Filter the HTML output of a taxonomy list.
567 * @param string $output HTML output.
568 * @param array $args An array of taxonomy-listing arguments.
570 $html = apply_filters( 'wp_list_categories', $output, $args );
582 * The text size is set by the 'smallest' and 'largest' arguments, which will
583 * use the 'unit' argument value for the CSS text size unit. The 'format'
584 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
585 * 'format' argument will separate tags with spaces. The list value for the
586 * 'format' argument will format the tags in a UL HTML list. The array value for
587 * the 'format' argument will return in PHP array type format.
589 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
590 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
592 * The 'number' argument is how many tags to return. By default, the limit will
593 * be to return the top 45 tags in the tag cloud list.
595 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
596 * text for the tooltip of the tag link.
598 * The 'topic_count_text_callback' argument is a function, which given the count
599 * of the posts with that tag returns a text for the tooltip of the tag link.
601 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
602 * passed to edit.php for the popular tags edit links.
604 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
605 * function. Only one should be used, because only one will be used and the
606 * other ignored, if they are both set.
610 * @param array|string|null $args Optional. Override default arguments.
611 * @return null|false Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
613 function wp_tag_cloud( $args = '' ) {
615 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
616 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
617 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
619 $args = wp_parse_args( $args, $defaults );
621 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
623 if ( empty( $tags ) || is_wp_error( $tags ) )
626 foreach ( $tags as $key => $tag ) {
627 if ( 'edit' == $args['link'] )
628 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
630 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
631 if ( is_wp_error( $link ) )
634 $tags[ $key ]->link = $link;
635 $tags[ $key ]->id = $tag->term_id;
638 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
641 * Filter the tag cloud output.
645 * @param string $return HTML output of the tag cloud.
646 * @param array $args An array of tag cloud arguments.
648 $return = apply_filters( 'wp_tag_cloud', $return, $args );
650 if ( 'array' == $args['format'] || empty($args['echo']) )
657 * Default topic count scaling for tag links
659 * @param integer $count number of posts with that tag
660 * @return integer scaled count
662 function default_topic_count_scale( $count ) {
663 return round(log10($count + 1) * 100);
667 * Generates a tag cloud (heatmap) from provided data.
669 * The text size is set by the 'smallest' and 'largest' arguments, which will
670 * use the 'unit' argument value for the CSS text size unit. The 'format'
671 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
672 * 'format' argument will separate tags with spaces. The list value for the
673 * 'format' argument will format the tags in a UL HTML list. The array value for
674 * the 'format' argument will return in PHP array type format.
676 * The 'tag_cloud_sort' filter allows you to override the sorting.
677 * Passed to the filter: $tags array and $args array, has to return the $tags array
680 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
681 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
684 * The 'number' argument is how many tags to return. By default, the limit will
685 * be to return the entire tag cloud list.
687 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
688 * text for the tooltip of the tag link.
690 * The 'topic_count_text_callback' argument is a function, which given the count
691 * of the posts with that tag returns a text for the tooltip of the tag link.
693 * @todo Complete functionality.
696 * @param array $tags List of tags.
697 * @param string|array $args Optional, override default arguments.
698 * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
700 function wp_generate_tag_cloud( $tags, $args = '' ) {
702 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
703 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
704 'topic_count_text' => null, 'topic_count_text_callback' => null,
705 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
708 $args = wp_parse_args( $args, $defaults );
710 $return = ( 'array' === $args['format'] ) ? array() : '';
712 if ( empty( $tags ) ) {
716 // Juggle topic count tooltips:
717 if ( isset( $args['topic_count_text'] ) ) {
718 // First look for nooped plural support via topic_count_text.
719 $translate_nooped_plural = $args['topic_count_text'];
720 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
721 // Look for the alternative callback style. Ignore the previous default.
722 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
723 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
725 $translate_nooped_plural = false;
727 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
728 // If no callback exists, look for the old-style single_text and multiple_text arguments.
729 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
731 // This is the default for when no callback, plural, or argument is passed in.
732 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
736 * Filter how the items in a tag cloud are sorted.
740 * @param array $tags Ordered array of terms.
741 * @param array $args An array of tag cloud arguments.
743 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
744 if ( empty( $tags_sorted ) ) {
748 if ( $tags_sorted !== $tags ) {
749 $tags = $tags_sorted;
750 unset( $tags_sorted );
752 if ( 'RAND' === $args['order'] ) {
755 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
756 if ( 'name' === $args['orderby'] ) {
757 uasort( $tags, '_wp_object_name_sort_cb' );
759 uasort( $tags, '_wp_object_count_sort_cb' );
762 if ( 'DESC' === $args['order'] ) {
763 $tags = array_reverse( $tags, true );
768 if ( $args['number'] > 0 )
769 $tags = array_slice( $tags, 0, $args['number'] );
772 $real_counts = array(); // For the alt tag
773 foreach ( (array) $tags as $key => $tag ) {
774 $real_counts[ $key ] = $tag->count;
775 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
778 $min_count = min( $counts );
779 $spread = max( $counts ) - $min_count;
782 $font_spread = $args['largest'] - $args['smallest'];
783 if ( $font_spread < 0 )
785 $font_step = $font_spread / $spread;
789 foreach ( $tags as $key => $tag ) {
790 $count = $counts[ $key ];
791 $real_count = $real_counts[ $key ];
792 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
793 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
794 $tag_name = $tags[ $key ]->name;
796 if ( $translate_nooped_plural ) {
797 $title_attribute = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
799 $title_attribute = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
802 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $title_attribute ) . "' style='font-size: " .
803 str_replace( ',', '.', ( $args['smallest'] + ( ( $count - $min_count ) * $font_step ) ) )
804 . $args['unit'] . ";'>$tag_name</a>";
807 switch ( $args['format'] ) {
812 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
813 $return .= join( "</li>\n\t<li>", $a );
814 $return .= "</li>\n</ul>\n";
817 $return = join( $args['separator'], $a );
821 if ( $args['filter'] ) {
823 * Filter the generated output of a tag cloud.
825 * The filter is only evaluated if a true value is passed
826 * to the $filter argument in wp_generate_tag_cloud().
830 * @see wp_generate_tag_cloud()
832 * @param array|string $return String containing the generated HTML tag cloud output
833 * or an array of tag links if the 'format' argument
835 * @param array $tags An array of terms used in the tag cloud.
836 * @param array $args An array of wp_generate_tag_cloud() arguments.
838 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
846 * Callback for comparing objects based on name
851 function _wp_object_name_sort_cb( $a, $b ) {
852 return strnatcasecmp( $a->name, $b->name );
856 * Callback for comparing objects based on count
861 function _wp_object_count_sort_cb( $a, $b ) {
862 return ( $a->count > $b->count );
870 * Retrieve HTML list content for category list.
872 * @uses Walker_Category to create HTML list content.
874 * @see Walker_Category::walk() for parameters and return description.
876 function walk_category_tree() {
877 $args = func_get_args();
878 // the user's options are the third parameter
879 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
880 $walker = new Walker_Category;
882 $walker = $args[2]['walker'];
884 return call_user_func_array(array( &$walker, 'walk' ), $args );
888 * Retrieve HTML dropdown (select) content for category list.
890 * @uses Walker_CategoryDropdown to create HTML dropdown content.
892 * @see Walker_CategoryDropdown::walk() for parameters and return description.
894 function walk_category_dropdown_tree() {
895 $args = func_get_args();
896 // the user's options are the third parameter
897 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
898 $walker = new Walker_CategoryDropdown;
900 $walker = $args[2]['walker'];
902 return call_user_func_array(array( &$walker, 'walk' ), $args );
906 * Create HTML list of categories.
912 class Walker_Category extends Walker {
914 * What the class handles.
916 * @see Walker::$tree_type
920 public $tree_type = 'category';
923 * Database fields to use.
925 * @see Walker::$db_fields
927 * @todo Decouple this
930 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
933 * Starts the list before the elements are added.
935 * @see Walker::start_lvl()
939 * @param string $output Passed by reference. Used to append additional content.
940 * @param int $depth Depth of category. Used for tab indentation.
941 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
942 * @see wp_list_categories()
944 public function start_lvl( &$output, $depth = 0, $args = array() ) {
945 if ( 'list' != $args['style'] )
948 $indent = str_repeat("\t", $depth);
949 $output .= "$indent<ul class='children'>\n";
953 * Ends the list of after the elements are added.
955 * @see Walker::end_lvl()
959 * @param string $output Passed by reference. Used to append additional content.
960 * @param int $depth Depth of category. Used for tab indentation.
961 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
962 * @wsee wp_list_categories()
964 public function end_lvl( &$output, $depth = 0, $args = array() ) {
965 if ( 'list' != $args['style'] )
968 $indent = str_repeat("\t", $depth);
969 $output .= "$indent</ul>\n";
973 * Start the element output.
975 * @see Walker::start_el()
979 * @param string $output Passed by reference. Used to append additional content.
980 * @param object $category Category data object.
981 * @param int $depth Depth of category in reference to parents. Default 0.
982 * @param array $args An array of arguments. @see wp_list_categories()
983 * @param int $id ID of the current category.
985 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
986 /** This filter is documented in wp-includes/category-template.php */
987 $cat_name = apply_filters(
989 esc_attr( $category->name ),
993 // Don't generate an element if the category name is empty.
998 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
999 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
1001 * Filter the category description for display.
1005 * @param string $description Category description.
1006 * @param object $category Category object.
1008 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
1012 $link .= $cat_name . '</a>';
1014 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
1017 if ( empty( $args['feed_image'] ) ) {
1021 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
1023 if ( empty( $args['feed'] ) ) {
1024 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
1026 $alt = ' alt="' . $args['feed'] . '"';
1027 $name = $args['feed'];
1028 $link .= empty( $args['title'] ) ? '' : $args['title'];
1033 if ( empty( $args['feed_image'] ) ) {
1036 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
1040 if ( empty( $args['feed_image'] ) ) {
1045 if ( ! empty( $args['show_count'] ) ) {
1046 $link .= ' (' . number_format_i18n( $category->count ) . ')';
1048 if ( 'list' == $args['style'] ) {
1050 $css_classes = array(
1052 'cat-item-' . $category->term_id,
1055 if ( ! empty( $args['current_category'] ) ) {
1056 $_current_category = get_term( $args['current_category'], $category->taxonomy );
1057 if ( $category->term_id == $args['current_category'] ) {
1058 $css_classes[] = 'current-cat';
1059 } elseif ( $category->term_id == $_current_category->parent ) {
1060 $css_classes[] = 'current-cat-parent';
1065 * Filter the list of CSS classes to include with each category in the list.
1069 * @see wp_list_categories()
1071 * @param array $css_classes An array of CSS classes to be applied to each list item.
1072 * @param object $category Category data object.
1073 * @param int $depth Depth of page, used for padding.
1074 * @param array $args An array of wp_list_categories() arguments.
1076 $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
1078 $output .= ' class="' . $css_classes . '"';
1079 $output .= ">$link\n";
1081 $output .= "\t$link<br />\n";
1086 * Ends the element output, if needed.
1088 * @see Walker::end_el()
1092 * @param string $output Passed by reference. Used to append additional content.
1093 * @param object $page Not used.
1094 * @param int $depth Depth of category. Not used.
1095 * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
1097 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
1098 if ( 'list' != $args['style'] )
1101 $output .= "</li>\n";
1107 * Create HTML dropdown list of Categories.
1109 * @package WordPress
1113 class Walker_CategoryDropdown extends Walker {
1115 * @see Walker::$tree_type
1119 public $tree_type = 'category';
1122 * @see Walker::$db_fields
1124 * @todo Decouple this
1127 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
1130 * Start the element output.
1132 * @see Walker::start_el()
1135 * @param string $output Passed by reference. Used to append additional content.
1136 * @param object $category Category data object.
1137 * @param int $depth Depth of category. Used for padding.
1138 * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
1139 * See {@see wp_dropdown_categories()}.
1141 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
1142 $pad = str_repeat(' ', $depth * 3);
1144 /** This filter is documented in wp-includes/category-template.php */
1145 $cat_name = apply_filters( 'list_cats', $category->name, $category );
1147 if ( ! isset( $args['value_field'] ) || ! isset( $category->{$args['value_field']} ) ) {
1148 $args['value_field'] = 'term_id';
1151 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$args['value_field']} ) . "\"";
1153 if ( $category->term_id == $args['selected'] )
1154 $output .= ' selected="selected"';
1156 $output .= $pad.$cat_name;
1157 if ( $args['show_count'] )
1158 $output .= ' ('. number_format_i18n( $category->count ) .')';
1159 $output .= "</option>\n";
1168 * Retrieve the link to the tag.
1171 * @see get_term_link()
1173 * @param int|object $tag Tag ID or object.
1174 * @return string Link on success, empty string if tag does not exist.
1176 function get_tag_link( $tag ) {
1177 if ( ! is_object( $tag ) )
1180 $tag = get_term_link( $tag, 'post_tag' );
1182 if ( is_wp_error( $tag ) )
1189 * Retrieve the tags for a post.
1193 * @param int $id Post ID.
1194 * @return array|bool Array of tag objects on success, false on failure.
1196 function get_the_tags( $id = 0 ) {
1199 * Filter the array of tags for the given post.
1203 * @see get_the_terms()
1205 * @param array $terms An array of tags for the given post.
1207 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1211 * Retrieve the tags for a post formatted as a string.
1215 * @param string $before Optional. Before tags.
1216 * @param string $sep Optional. Between tags.
1217 * @param string $after Optional. After tags.
1218 * @param int $id Optional. Post ID. Defaults to the current post.
1219 * @return string|bool|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
1221 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1224 * Filter the tags list for a given post.
1228 * @param string $tag_list List of tags.
1229 * @param string $before String to use before tags.
1230 * @param string $sep String to use between the tags.
1231 * @param string $after String to use after tags.
1232 * @param int $id Post ID.
1234 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1238 * Retrieve the tags for a post.
1242 * @param string $before Optional. Before list.
1243 * @param string $sep Optional. Separate items using this.
1244 * @param string $after Optional. After list.
1246 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1247 if ( null === $before )
1248 $before = __('Tags: ');
1249 echo get_the_tag_list($before, $sep, $after);
1253 * Retrieve tag description.
1257 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1258 * @return string Tag description, available.
1260 function tag_description( $tag = 0 ) {
1261 return term_description( $tag );
1265 * Retrieve term description.
1269 * @param int $term Optional. Term ID. Will use global term ID by default.
1270 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1271 * @return string Term description, available.
1273 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1274 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1275 $term = get_queried_object();
1277 $taxonomy = $term->taxonomy;
1278 $term = $term->term_id;
1281 $description = get_term_field( 'description', $term, $taxonomy );
1282 return is_wp_error( $description ) ? '' : $description;
1286 * Retrieve the terms of the taxonomy that are attached to the post.
1290 * @param int|object $post Post ID or object.
1291 * @param string $taxonomy Taxonomy name.
1292 * @return array|bool|WP_Error Array of term objects on success, false if there are no terms
1293 * or the post does not exist, WP_Error on failure.
1295 function get_the_terms( $post, $taxonomy ) {
1296 if ( ! $post = get_post( $post ) )
1299 $terms = get_object_term_cache( $post->ID, $taxonomy );
1300 if ( false === $terms ) {
1301 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1302 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
1306 * Filter the list of terms attached to the given post.
1310 * @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
1311 * @param int $post_id Post ID.
1312 * @param string $taxonomy Name of the taxonomy.
1314 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1316 if ( empty( $terms ) )
1323 * Retrieve a post's terms as a list with specified format.
1327 * @param int $id Post ID.
1328 * @param string $taxonomy Taxonomy name.
1329 * @param string $before Optional. Before list.
1330 * @param string $sep Optional. Separate items using this.
1331 * @param string $after Optional. After list.
1332 * @return string|bool|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
1334 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1335 $terms = get_the_terms( $id, $taxonomy );
1337 if ( is_wp_error( $terms ) )
1340 if ( empty( $terms ) )
1345 foreach ( $terms as $term ) {
1346 $link = get_term_link( $term, $taxonomy );
1347 if ( is_wp_error( $link ) ) {
1350 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1354 * Filter the term links for a given taxonomy.
1356 * The dynamic portion of the filter name, `$taxonomy`, refers
1357 * to the taxonomy slug.
1361 * @param array $links An array of term links.
1363 $term_links = apply_filters( "term_links-$taxonomy", $links );
1365 return $before . join( $sep, $term_links ) . $after;
1369 * Display the terms in a list.
1373 * @param int $id Post ID.
1374 * @param string $taxonomy Taxonomy name.
1375 * @param string $before Optional. Before list.
1376 * @param string $sep Optional. Separate items using this.
1377 * @param string $after Optional. After list.
1378 * @return false|null False on WordPress error. Returns null when displaying.
1380 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1381 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1383 if ( is_wp_error( $term_list ) )
1387 * Filter the list of terms to display.
1391 * @param array $term_list List of terms to display.
1392 * @param string $taxonomy The taxonomy name.
1393 * @param string $before String to use before the terms.
1394 * @param string $sep String to use between the terms.
1395 * @param string $after String to use after the terms.
1397 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
1401 * Check if the current post has any of given category.
1405 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1406 * @param int|object $post Optional. Post to check instead of the current post.
1407 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1409 function has_category( $category = '', $post = null ) {
1410 return has_term( $category, 'category', $post );
1414 * Check if the current post has any of given tags.
1416 * The given tags are checked against the post's tags' term_ids, names and slugs.
1417 * Tags given as integers will only be checked against the post's tags' term_ids.
1418 * If no tags are given, determines if post has any tags.
1420 * 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)
1421 * Prior to v2.7, this function could only be used in the WordPress Loop.
1422 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1426 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1427 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1428 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1430 function has_tag( $tag = '', $post = null ) {
1431 return has_term( $tag, 'post_tag', $post );
1435 * Check if the current post has any of given terms.
1437 * The given terms are checked against the post's terms' term_ids, names and slugs.
1438 * Terms given as integers will only be checked against the post's terms' term_ids.
1439 * If no terms are given, determines if post has any terms.
1443 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1444 * @param string $taxonomy Taxonomy name
1445 * @param int|object $post Optional. Post to check instead of the current post.
1446 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1448 function has_term( $term = '', $taxonomy = '', $post = null ) {
1449 $post = get_post($post);
1454 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1455 if ( is_wp_error( $r ) )