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.
68 * This tag may be used outside The Loop by passing a post id as the parameter.
70 * Note: This function only returns results from the default "category" taxonomy.
71 * For custom taxonomies use get_the_terms().
75 * @param int $id Optional, default to current post ID. The post ID.
76 * @return array Array of objects, one for each category assigned to the post.
78 function get_the_category( $id = false ) {
79 $categories = get_the_terms( $id, 'category' );
80 if ( ! $categories || is_wp_error( $categories ) )
81 $categories = array();
83 $categories = array_values( $categories );
85 foreach ( array_keys( $categories ) as $key ) {
86 _make_cat_compat( $categories[$key] );
90 * Filter the array of categories to return for a post.
94 * @param array $categories An array of categories to return for the post.
96 return apply_filters( 'get_the_categories', $categories );
100 * Sort categories by name.
102 * Used by usort() as a callback, should not be used directly. Can actually be
103 * used to sort any term object.
112 function _usort_terms_by_name( $a, $b ) {
113 return strcmp( $a->name, $b->name );
117 * Sort categories by ID.
119 * Used by usort() as a callback, should not be used directly. Can actually be
120 * used to sort any term object.
129 function _usort_terms_by_ID( $a, $b ) {
130 if ( $a->term_id > $b->term_id )
132 elseif ( $a->term_id < $b->term_id )
139 * Retrieve category name based on category ID.
143 * @param int $cat_ID Category ID.
144 * @return string|WP_Error Category name on success, WP_Error on failure.
146 function get_the_category_by_ID( $cat_ID ) {
147 $cat_ID = (int) $cat_ID;
148 $category = get_term( $cat_ID, 'category' );
150 if ( is_wp_error( $category ) )
153 return ( $category ) ? $category->name : '';
157 * Retrieve category list in either HTML list or custom format.
161 * @global WP_Rewrite $wp_rewrite
163 * @param string $separator Optional, default is empty string. Separator for between the categories.
164 * @param string $parents Optional. How to display the parents.
165 * @param int $post_id Optional. Post ID to retrieve categories.
168 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
170 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) ) {
171 /** This filter is documented in wp-includes/category-template.php */
172 return apply_filters( 'the_category', '', $separator, $parents );
175 $categories = get_the_category( $post_id );
176 if ( empty( $categories ) ) {
177 /** This filter is documented in wp-includes/category-template.php */
178 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
181 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
184 if ( '' == $separator ) {
185 $thelist .= '<ul class="post-categories">';
186 foreach ( $categories as $category ) {
187 $thelist .= "\n\t<li>";
188 switch ( strtolower( $parents ) ) {
190 if ( $category->parent )
191 $thelist .= get_category_parents( $category->parent, true, $separator );
192 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
195 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
196 if ( $category->parent )
197 $thelist .= get_category_parents( $category->parent, false, $separator );
198 $thelist .= $category->name.'</a></li>';
202 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
208 foreach ( $categories as $category ) {
210 $thelist .= $separator;
211 switch ( strtolower( $parents ) ) {
213 if ( $category->parent )
214 $thelist .= get_category_parents( $category->parent, true, $separator );
215 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
218 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>';
219 if ( $category->parent )
220 $thelist .= get_category_parents( $category->parent, false, $separator );
221 $thelist .= "$category->name</a>";
225 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>';
232 * Filter the category or list of categories.
236 * @param array $thelist List of categories for the current post.
237 * @param string $separator Separator used between the categories.
238 * @param string $parents How to display the category parents. Accepts 'multiple',
239 * 'single', or empty.
241 return apply_filters( 'the_category', $thelist, $separator, $parents );
245 * Check if the current post in within any of the given categories.
247 * The given categories are checked against the post's categories' term_ids, names and slugs.
248 * Categories given as integers will only be checked against the post's categories' term_ids.
250 * Prior to v2.5 of WordPress, category names were not supported.
251 * Prior to v2.7, category slugs were not supported.
252 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
253 * Prior to v2.7, this function could only be used in the WordPress Loop.
254 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
258 * @param int|string|array $category Category ID, name or slug, or array of said.
259 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
260 * @return bool True if the current post is in any of the given categories.
262 function in_category( $category, $post = null ) {
263 if ( empty( $category ) )
266 return has_category( $category, $post );
270 * Display the category list for the post.
274 * @param string $separator Optional, default is empty string. Separator for between the categories.
275 * @param string $parents Optional. How to display the parents.
276 * @param int $post_id Optional. Post ID to retrieve categories.
278 function the_category( $separator = '', $parents='', $post_id = false ) {
279 echo get_the_category_list( $separator, $parents, $post_id );
283 * Retrieve category description.
287 * @param int $category Optional. Category ID. Will use global category ID by default.
288 * @return string Category description, available.
290 function category_description( $category = 0 ) {
291 return term_description( $category, 'category' );
295 * Display or retrieve the HTML dropdown list of categories.
297 * The 'hierarchical' argument, which is disabled by default, will override the
298 * depth argument, unless it is true. When the argument is false, it will
299 * display all of the categories. When it is enabled it will use the value in
300 * the 'depth' argument.
303 * @since 4.2.0 Introduced the `value_field` argument.
305 * @param string|array $args {
306 * Optional. Array or string of arguments to generate a categories drop-down element.
308 * @type string $show_option_all Text to display for showing all categories. Default empty.
309 * @type string $show_option_none Text to display for showing no categories. Default empty.
310 * @type string $option_none_value Value to use when no category is selected. Default empty.
311 * @type string $orderby Which column to use for ordering categories. See get_terms() for a list
312 * of accepted values. Default 'id' (term_id).
313 * @type string $order Whether to order terms in ascending or descending order. Accepts 'ASC'
314 * or 'DESC'. Default 'ASC'.
315 * @type bool $pad_counts See get_terms() for an argument description. Default false.
316 * @type bool|int $show_count Whether to include post counts. Accepts 0, 1, or their bool equivalents.
318 * @type bool|int $hide_empty Whether to hide categories that don't have any posts. Accepts 0, 1, or
319 * their bool equivalents. Default 1.
320 * @type int $child_of Term ID to retrieve child terms of. See get_terms(). Default 0.
321 * @type array|string $exclude Array or comma/space-separated string of term ids to exclude.
322 * If `$include` is non-empty, `$exclude` is ignored. Default empty array.
323 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their
324 * bool equivalents. Default 1.
325 * @type bool|int $hierarchical Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool
326 * equivalents. Default 0.
327 * @type int $depth Maximum depth. Default 0.
328 * @type int $tab_index Tab index for the select element. Default 0 (no tabindex).
329 * @type string $name Value for the 'name' attribute of the select element. Default 'cat'.
330 * @type string $id Value for the 'id' attribute of the select element. Defaults to the value
332 * @type string $class Value for the 'class' attribute of the select element. Default 'postform'.
333 * @type int|string $selected Value of the option that should be selected. Default 0.
334 * @type string $value_field Term field that should be used to populate the 'value' attribute
335 * of the option elements. Accepts any valid term field: 'term_id', 'name',
336 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description',
337 * 'parent', 'count'. Default 'term_id'.
338 * @type string $taxonomy Name of the category to retrieve. Default 'category'.
339 * @type bool $hide_if_empty True to skip generating markup if no categories are found.
340 * Default false (create select element even if no categories are found).
342 * @return string HTML content only if 'echo' argument is 0.
344 function wp_dropdown_categories( $args = '' ) {
346 'show_option_all' => '', 'show_option_none' => '',
347 'orderby' => 'id', 'order' => 'ASC',
349 'hide_empty' => 1, 'child_of' => 0,
350 'exclude' => '', 'echo' => 1,
351 'selected' => 0, 'hierarchical' => 0,
352 'name' => 'cat', 'id' => '',
353 'class' => 'postform', 'depth' => 0,
354 'tab_index' => 0, 'taxonomy' => 'category',
355 'hide_if_empty' => false, 'option_none_value' => -1,
356 'value_field' => 'term_id',
359 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
362 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
363 _deprecated_argument( __FUNCTION__, '3.0', '' );
364 $args['taxonomy'] = 'link_category';
367 $r = wp_parse_args( $args, $defaults );
368 $option_none_value = $r['option_none_value'];
370 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
371 $r['pad_counts'] = true;
374 $tab_index = $r['tab_index'];
376 $tab_index_attribute = '';
377 if ( (int) $tab_index > 0 ) {
378 $tab_index_attribute = " tabindex=\"$tab_index\"";
381 // Avoid clashes with the 'name' param of get_terms().
382 $get_terms_args = $r;
383 unset( $get_terms_args['name'] );
384 $categories = get_terms( $r['taxonomy'], $get_terms_args );
386 $name = esc_attr( $r['name'] );
387 $class = esc_attr( $r['class'] );
388 $id = $r['id'] ? esc_attr( $r['id'] ) : $name;
390 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
391 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
395 if ( empty( $categories ) && ! $r['hide_if_empty'] && ! empty( $r['show_option_none'] ) ) {
398 * Filter a taxonomy drop-down display element.
400 * A variety of taxonomy drop-down display elements can be modified
401 * just prior to display via this filter. Filterable arguments include
402 * 'show_option_none', 'show_option_all', and various forms of the
407 * @see wp_dropdown_categories()
409 * @param string $element Taxonomy element to list.
411 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
412 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n";
415 if ( ! empty( $categories ) ) {
417 if ( $r['show_option_all'] ) {
419 /** This filter is documented in wp-includes/category-template.php */
420 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'] );
421 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
422 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
425 if ( $r['show_option_none'] ) {
427 /** This filter is documented in wp-includes/category-template.php */
428 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'] );
429 $selected = selected( $option_none_value, $r['selected'], false );
430 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n";
433 if ( $r['hierarchical'] ) {
434 $depth = $r['depth']; // Walk the full depth.
436 $depth = -1; // Flat.
438 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
441 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) {
442 $output .= "</select>\n";
445 * Filter the taxonomy drop-down output.
449 * @param string $output HTML output.
450 * @param array $r Arguments used to build the drop-down.
452 $output = apply_filters( 'wp_dropdown_cats', $output, $r );
461 * Display or retrieve the HTML list of categories.
463 * The list of arguments is below:
464 * 'show_option_all' (string) - Text to display for showing all categories.
465 * 'orderby' (string) default is 'ID' - What column to use for ordering the
467 * 'order' (string) default is 'ASC' - What direction to order categories.
468 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
470 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
471 * don't have any posts attached to them.
472 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
473 * category description as the title attribute.
474 * 'feed' - See {@link get_categories()}.
475 * 'feed_type' - See {@link get_categories()}.
476 * 'feed_image' - See {@link get_categories()}.
477 * 'child_of' (int) default is 0 - See {@link get_categories()}.
478 * 'exclude' (string) - See {@link get_categories()}.
479 * 'exclude_tree' (string) - See {@link get_categories()}.
480 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
481 * 'current_category' (int) - See {@link get_categories()}.
482 * 'hierarchical' (bool) - See {@link get_categories()}.
483 * 'title_li' (string) - See {@link get_categories()}.
484 * 'depth' (int) - The max depth.
488 * @param string|array $args Optional. Override default arguments.
489 * @return false|string HTML content only if 'echo' argument is 0.
491 function wp_list_categories( $args = '' ) {
493 'show_option_all' => '', 'show_option_none' => __('No categories'),
494 'orderby' => 'name', 'order' => 'ASC',
496 'show_count' => 0, 'hide_empty' => 1,
497 'use_desc_for_title' => 1, 'child_of' => 0,
498 'feed' => '', 'feed_type' => '',
499 'feed_image' => '', 'exclude' => '',
500 'exclude_tree' => '', 'current_category' => 0,
501 'hierarchical' => true, 'title_li' => __( 'Categories' ),
502 'echo' => 1, 'depth' => 0,
503 'taxonomy' => 'category'
506 $r = wp_parse_args( $args, $defaults );
508 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
509 $r['pad_counts'] = true;
511 if ( true == $r['hierarchical'] ) {
512 $r['exclude_tree'] = $r['exclude'];
516 if ( ! isset( $r['class'] ) )
517 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
519 if ( ! taxonomy_exists( $r['taxonomy'] ) ) {
523 $show_option_all = $r['show_option_all'];
524 $show_option_none = $r['show_option_none'];
526 $categories = get_categories( $r );
529 if ( $r['title_li'] && 'list' == $r['style'] ) {
530 $output = '<li class="' . esc_attr( $r['class'] ) . '">' . $r['title_li'] . '<ul>';
532 if ( empty( $categories ) ) {
533 if ( ! empty( $show_option_none ) ) {
534 if ( 'list' == $r['style'] ) {
535 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
537 $output .= $show_option_none;
541 if ( ! empty( $show_option_all ) ) {
545 // For taxonomies that belong only to custom post types, point to a valid archive.
546 $taxonomy_object = get_taxonomy( $r['taxonomy'] );
547 if ( ! in_array( 'post', $taxonomy_object->object_type ) && ! in_array( 'page', $taxonomy_object->object_type ) ) {
548 foreach ( $taxonomy_object->object_type as $object_type ) {
549 $_object_type = get_post_type_object( $object_type );
551 // Grab the first one.
552 if ( ! empty( $_object_type->has_archive ) ) {
553 $posts_page = get_post_type_archive_link( $object_type );
559 // Fallback for the 'All' link is the front page.
560 if ( ! $posts_page ) {
561 $posts_page = 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
564 $posts_page = esc_url( $posts_page );
565 if ( 'list' == $r['style'] ) {
566 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
568 $output .= "<a href='$posts_page'>$show_option_all</a>";
572 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
573 $current_term_object = get_queried_object();
574 if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy ) {
575 $r['current_category'] = get_queried_object_id();
579 if ( $r['hierarchical'] ) {
580 $depth = $r['depth'];
582 $depth = -1; // Flat.
584 $output .= walk_category_tree( $categories, $depth, $r );
587 if ( $r['title_li'] && 'list' == $r['style'] )
588 $output .= '</ul></li>';
591 * Filter the HTML output of a taxonomy list.
595 * @param string $output HTML output.
596 * @param array $args An array of taxonomy-listing arguments.
598 $html = apply_filters( 'wp_list_categories', $output, $args );
610 * The text size is set by the 'smallest' and 'largest' arguments, which will
611 * use the 'unit' argument value for the CSS text size unit. The 'format'
612 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
613 * 'format' argument will separate tags with spaces. The list value for the
614 * 'format' argument will format the tags in a UL HTML list. The array value for
615 * the 'format' argument will return in PHP array type format.
617 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
618 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
620 * The 'number' argument is how many tags to return. By default, the limit will
621 * be to return the top 45 tags in the tag cloud list.
623 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
624 * text for the tooltip of the tag link.
626 * The 'topic_count_text_callback' argument is a function, which given the count
627 * of the posts with that tag returns a text for the tooltip of the tag link.
629 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type
630 * passed to edit.php for the popular tags edit links.
632 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
633 * function. Only one should be used, because only one will be used and the
634 * other ignored, if they are both set.
638 * @param array|string|null $args Optional. Override default arguments.
639 * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
640 * Otherwise, this function outputs the tag cloud.
642 function wp_tag_cloud( $args = '' ) {
644 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
645 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
646 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true
648 $args = wp_parse_args( $args, $defaults );
650 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
652 if ( empty( $tags ) || is_wp_error( $tags ) )
655 foreach ( $tags as $key => $tag ) {
656 if ( 'edit' == $args['link'] )
657 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] );
659 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
660 if ( is_wp_error( $link ) )
663 $tags[ $key ]->link = $link;
664 $tags[ $key ]->id = $tag->term_id;
667 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
670 * Filter the tag cloud output.
674 * @param string $return HTML output of the tag cloud.
675 * @param array $args An array of tag cloud arguments.
677 $return = apply_filters( 'wp_tag_cloud', $return, $args );
679 if ( 'array' == $args['format'] || empty($args['echo']) )
686 * Default topic count scaling for tag links
688 * @param int $count number of posts with that tag
689 * @return int scaled count
691 function default_topic_count_scale( $count ) {
692 return round(log10($count + 1) * 100);
696 * Generates a tag cloud (heatmap) from provided data.
698 * The text size is set by the 'smallest' and 'largest' arguments, which will
699 * use the 'unit' argument value for the CSS text size unit. The 'format'
700 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
701 * 'format' argument will separate tags with spaces. The list value for the
702 * 'format' argument will format the tags in a UL HTML list. The array value for
703 * the 'format' argument will return in PHP array type format.
705 * The 'tag_cloud_sort' filter allows you to override the sorting.
706 * Passed to the filter: $tags array and $args array, has to return the $tags array
709 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
710 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
713 * The 'number' argument is how many tags to return. By default, the limit will
714 * be to return the entire tag cloud list.
716 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the
717 * text for the tooltip of the tag link.
719 * The 'topic_count_text_callback' argument is a function, which given the count
720 * of the posts with that tag returns a text for the tooltip of the tag link.
722 * @todo Complete functionality.
725 * @param array $tags List of tags.
726 * @param string|array $args Optional, override default arguments.
727 * @return string|array Tag cloud as a string or an array, depending on 'format' argument.
729 function wp_generate_tag_cloud( $tags, $args = '' ) {
731 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
732 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
733 'topic_count_text' => null, 'topic_count_text_callback' => null,
734 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
737 $args = wp_parse_args( $args, $defaults );
739 $return = ( 'array' === $args['format'] ) ? array() : '';
741 if ( empty( $tags ) ) {
745 // Juggle topic count tooltips:
746 if ( isset( $args['topic_count_text'] ) ) {
747 // First look for nooped plural support via topic_count_text.
748 $translate_nooped_plural = $args['topic_count_text'];
749 } elseif ( ! empty( $args['topic_count_text_callback'] ) ) {
750 // Look for the alternative callback style. Ignore the previous default.
751 if ( $args['topic_count_text_callback'] === 'default_topic_count_text' ) {
752 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
754 $translate_nooped_plural = false;
756 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
757 // If no callback exists, look for the old-style single_text and multiple_text arguments.
758 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] );
760 // This is the default for when no callback, plural, or argument is passed in.
761 $translate_nooped_plural = _n_noop( '%s topic', '%s topics' );
765 * Filter how the items in a tag cloud are sorted.
769 * @param array $tags Ordered array of terms.
770 * @param array $args An array of tag cloud arguments.
772 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
773 if ( empty( $tags_sorted ) ) {
777 if ( $tags_sorted !== $tags ) {
778 $tags = $tags_sorted;
779 unset( $tags_sorted );
781 if ( 'RAND' === $args['order'] ) {
784 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
785 if ( 'name' === $args['orderby'] ) {
786 uasort( $tags, '_wp_object_name_sort_cb' );
788 uasort( $tags, '_wp_object_count_sort_cb' );
791 if ( 'DESC' === $args['order'] ) {
792 $tags = array_reverse( $tags, true );
797 if ( $args['number'] > 0 )
798 $tags = array_slice( $tags, 0, $args['number'] );
801 $real_counts = array(); // For the alt tag
802 foreach ( (array) $tags as $key => $tag ) {
803 $real_counts[ $key ] = $tag->count;
804 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count );
807 $min_count = min( $counts );
808 $spread = max( $counts ) - $min_count;
811 $font_spread = $args['largest'] - $args['smallest'];
812 if ( $font_spread < 0 )
814 $font_step = $font_spread / $spread;
816 // Assemble the data that will be used to generate the tag cloud markup.
817 $tags_data = array();
818 foreach ( $tags as $key => $tag ) {
819 $tag_id = isset( $tag->id ) ? $tag->id : $key;
821 $count = $counts[ $key ];
822 $real_count = $real_counts[ $key ];
824 if ( $translate_nooped_plural ) {
825 $title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
827 $title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
830 $tags_data[] = array(
832 'url' => '#' != $tag->link ? $tag->link : '#',
833 'name' => $tag->name,
835 'slug' => $tag->slug,
836 'real_count' => $real_count,
837 'class' => 'tag-link-' . $tag_id,
838 'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step,
843 * Filter the data used to generate the tag cloud.
847 * @param array $tags_data An array of term data for term used to generate the tag cloud.
849 $tags_data = apply_filters( 'wp_generate_tag_cloud_data', $tags_data );
853 // generate the output links array
854 foreach ( $tags_data as $key => $tag_data ) {
855 $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>";
858 switch ( $args['format'] ) {
863 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
864 $return .= join( "</li>\n\t<li>", $a );
865 $return .= "</li>\n</ul>\n";
868 $return = join( $args['separator'], $a );
872 if ( $args['filter'] ) {
874 * Filter the generated output of a tag cloud.
876 * The filter is only evaluated if a true value is passed
877 * to the $filter argument in wp_generate_tag_cloud().
881 * @see wp_generate_tag_cloud()
883 * @param array|string $return String containing the generated HTML tag cloud output
884 * or an array of tag links if the 'format' argument
886 * @param array $tags An array of terms used in the tag cloud.
887 * @param array $args An array of wp_generate_tag_cloud() arguments.
889 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
897 * Callback for comparing objects based on name
903 function _wp_object_name_sort_cb( $a, $b ) {
904 return strnatcasecmp( $a->name, $b->name );
908 * Callback for comparing objects based on count
914 function _wp_object_count_sort_cb( $a, $b ) {
915 return ( $a->count > $b->count );
923 * Retrieve HTML list content for category list.
925 * @uses Walker_Category to create HTML list content.
927 * @see Walker_Category::walk() for parameters and return description.
930 function walk_category_tree() {
931 $args = func_get_args();
932 // the user's options are the third parameter
933 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
934 $walker = new Walker_Category;
936 $walker = $args[2]['walker'];
938 return call_user_func_array( array( $walker, 'walk' ), $args );
942 * Retrieve HTML dropdown (select) content for category list.
944 * @uses Walker_CategoryDropdown to create HTML dropdown content.
946 * @see Walker_CategoryDropdown::walk() for parameters and return description.
949 function walk_category_dropdown_tree() {
950 $args = func_get_args();
951 // the user's options are the third parameter
952 if ( empty( $args[2]['walker'] ) || ! ( $args[2]['walker'] instanceof Walker ) ) {
953 $walker = new Walker_CategoryDropdown;
955 $walker = $args[2]['walker'];
957 return call_user_func_array( array( $walker, 'walk' ), $args );
961 * Create HTML list of categories.
967 class Walker_Category extends Walker {
969 * What the class handles.
971 * @see Walker::$tree_type
975 public $tree_type = 'category';
978 * Database fields to use.
980 * @see Walker::$db_fields
982 * @todo Decouple this
985 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
988 * Starts the list before the elements are added.
990 * @see Walker::start_lvl()
994 * @param string $output Passed by reference. Used to append additional content.
995 * @param int $depth Depth of category. Used for tab indentation.
996 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
997 * @see wp_list_categories()
999 public function start_lvl( &$output, $depth = 0, $args = array() ) {
1000 if ( 'list' != $args['style'] )
1003 $indent = str_repeat("\t", $depth);
1004 $output .= "$indent<ul class='children'>\n";
1008 * Ends the list of after the elements are added.
1010 * @see Walker::end_lvl()
1014 * @param string $output Passed by reference. Used to append additional content.
1015 * @param int $depth Depth of category. Used for tab indentation.
1016 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
1017 * @wsee wp_list_categories()
1019 public function end_lvl( &$output, $depth = 0, $args = array() ) {
1020 if ( 'list' != $args['style'] )
1023 $indent = str_repeat("\t", $depth);
1024 $output .= "$indent</ul>\n";
1028 * Start the element output.
1030 * @see Walker::start_el()
1034 * @param string $output Passed by reference. Used to append additional content.
1035 * @param object $category Category data object.
1036 * @param int $depth Depth of category in reference to parents. Default 0.
1037 * @param array $args An array of arguments. @see wp_list_categories()
1038 * @param int $id ID of the current category.
1040 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
1041 /** This filter is documented in wp-includes/category-template.php */
1042 $cat_name = apply_filters(
1044 esc_attr( $category->name ),
1048 // Don't generate an element if the category name is empty.
1049 if ( ! $cat_name ) {
1053 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
1054 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
1056 * Filter the category description for display.
1060 * @param string $description Category description.
1061 * @param object $category Category object.
1063 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
1067 $link .= $cat_name . '</a>';
1069 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
1072 if ( empty( $args['feed_image'] ) ) {
1076 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
1078 if ( empty( $args['feed'] ) ) {
1079 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
1081 $alt = ' alt="' . $args['feed'] . '"';
1082 $name = $args['feed'];
1083 $link .= empty( $args['title'] ) ? '' : $args['title'];
1088 if ( empty( $args['feed_image'] ) ) {
1091 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
1095 if ( empty( $args['feed_image'] ) ) {
1100 if ( ! empty( $args['show_count'] ) ) {
1101 $link .= ' (' . number_format_i18n( $category->count ) . ')';
1103 if ( 'list' == $args['style'] ) {
1105 $css_classes = array(
1107 'cat-item-' . $category->term_id,
1110 if ( ! empty( $args['current_category'] ) ) {
1111 $_current_category = get_term( $args['current_category'], $category->taxonomy );
1112 if ( $category->term_id == $args['current_category'] ) {
1113 $css_classes[] = 'current-cat';
1114 } elseif ( $category->term_id == $_current_category->parent ) {
1115 $css_classes[] = 'current-cat-parent';
1120 * Filter the list of CSS classes to include with each category in the list.
1124 * @see wp_list_categories()
1126 * @param array $css_classes An array of CSS classes to be applied to each list item.
1127 * @param object $category Category data object.
1128 * @param int $depth Depth of page, used for padding.
1129 * @param array $args An array of wp_list_categories() arguments.
1131 $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
1133 $output .= ' class="' . $css_classes . '"';
1134 $output .= ">$link\n";
1136 $output .= "\t$link<br />\n";
1141 * Ends the element output, if needed.
1143 * @see Walker::end_el()
1147 * @param string $output Passed by reference. Used to append additional content.
1148 * @param object $page Not used.
1149 * @param int $depth Depth of category. Not used.
1150 * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
1152 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
1153 if ( 'list' != $args['style'] )
1156 $output .= "</li>\n";
1162 * Create HTML dropdown list of Categories.
1164 * @package WordPress
1168 class Walker_CategoryDropdown extends Walker {
1170 * @see Walker::$tree_type
1174 public $tree_type = 'category';
1177 * @see Walker::$db_fields
1179 * @todo Decouple this
1182 public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
1185 * Start the element output.
1187 * @see Walker::start_el()
1190 * @param string $output Passed by reference. Used to append additional content.
1191 * @param object $category Category data object.
1192 * @param int $depth Depth of category. Used for padding.
1193 * @param array $args Uses 'selected', 'show_count', and 'value_field' keys, if they exist.
1194 * See {@see wp_dropdown_categories()}.
1196 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
1197 $pad = str_repeat(' ', $depth * 3);
1199 /** This filter is documented in wp-includes/category-template.php */
1200 $cat_name = apply_filters( 'list_cats', $category->name, $category );
1202 if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) {
1203 $value_field = $args['value_field'];
1205 $value_field = 'term_id';
1208 $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\"";
1210 if ( $category->{$value_field} == $args['selected'] )
1211 $output .= ' selected="selected"';
1213 $output .= $pad.$cat_name;
1214 if ( $args['show_count'] )
1215 $output .= ' ('. number_format_i18n( $category->count ) .')';
1216 $output .= "</option>\n";
1225 * Retrieve the link to the tag.
1228 * @see get_term_link()
1230 * @param int|object $tag Tag ID or object.
1231 * @return string Link on success, empty string if tag does not exist.
1233 function get_tag_link( $tag ) {
1234 if ( ! is_object( $tag ) )
1237 $tag = get_term_link( $tag, 'post_tag' );
1239 if ( is_wp_error( $tag ) )
1246 * Retrieve the tags for a post.
1250 * @param int $id Post ID.
1251 * @return array|false|WP_Error Array of tag objects on success, false on failure.
1253 function get_the_tags( $id = 0 ) {
1256 * Filter the array of tags for the given post.
1260 * @see get_the_terms()
1262 * @param array $terms An array of tags for the given post.
1264 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1268 * Retrieve the tags for a post formatted as a string.
1272 * @param string $before Optional. Before tags.
1273 * @param string $sep Optional. Between tags.
1274 * @param string $after Optional. After tags.
1275 * @param int $id Optional. Post ID. Defaults to the current post.
1276 * @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
1278 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1281 * Filter the tags list for a given post.
1285 * @param string $tag_list List of tags.
1286 * @param string $before String to use before tags.
1287 * @param string $sep String to use between the tags.
1288 * @param string $after String to use after tags.
1289 * @param int $id Post ID.
1291 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1295 * Retrieve the tags for a post.
1299 * @param string $before Optional. Before list.
1300 * @param string $sep Optional. Separate items using this.
1301 * @param string $after Optional. After list.
1303 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1304 if ( null === $before )
1305 $before = __('Tags: ');
1306 echo get_the_tag_list($before, $sep, $after);
1310 * Retrieve tag description.
1314 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1315 * @return string Tag description, available.
1317 function tag_description( $tag = 0 ) {
1318 return term_description( $tag );
1322 * Retrieve term description.
1326 * @param int $term Optional. Term ID. Will use global term ID by default.
1327 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1328 * @return string Term description, available.
1330 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1331 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1332 $term = get_queried_object();
1334 $taxonomy = $term->taxonomy;
1335 $term = $term->term_id;
1338 $description = get_term_field( 'description', $term, $taxonomy );
1339 return is_wp_error( $description ) ? '' : $description;
1343 * Retrieve the terms of the taxonomy that are attached to the post.
1347 * @param int|object $post Post ID or object.
1348 * @param string $taxonomy Taxonomy name.
1349 * @return array|false|WP_Error Array of term objects on success, false if there are no terms
1350 * or the post does not exist, WP_Error on failure.
1352 function get_the_terms( $post, $taxonomy ) {
1353 if ( ! $post = get_post( $post ) )
1356 $terms = get_object_term_cache( $post->ID, $taxonomy );
1357 if ( false === $terms ) {
1358 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1359 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
1363 * Filter the list of terms attached to the given post.
1367 * @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
1368 * @param int $post_id Post ID.
1369 * @param string $taxonomy Name of the taxonomy.
1371 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1373 if ( empty( $terms ) )
1380 * Retrieve a post's terms as a list with specified format.
1384 * @param int $id Post ID.
1385 * @param string $taxonomy Taxonomy name.
1386 * @param string $before Optional. Before list.
1387 * @param string $sep Optional. Separate items using this.
1388 * @param string $after Optional. After list.
1389 * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure.
1391 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1392 $terms = get_the_terms( $id, $taxonomy );
1394 if ( is_wp_error( $terms ) )
1397 if ( empty( $terms ) )
1402 foreach ( $terms as $term ) {
1403 $link = get_term_link( $term, $taxonomy );
1404 if ( is_wp_error( $link ) ) {
1407 $links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1411 * Filter the term links for a given taxonomy.
1413 * The dynamic portion of the filter name, `$taxonomy`, refers
1414 * to the taxonomy slug.
1418 * @param array $links An array of term links.
1420 $term_links = apply_filters( "term_links-$taxonomy", $links );
1422 return $before . join( $sep, $term_links ) . $after;
1426 * Display the terms in a list.
1430 * @param int $id Post ID.
1431 * @param string $taxonomy Taxonomy name.
1432 * @param string $before Optional. Before list.
1433 * @param string $sep Optional. Separate items using this.
1434 * @param string $after Optional. After list.
1435 * @return false|void False on WordPress error.
1437 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1438 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1440 if ( is_wp_error( $term_list ) )
1444 * Filter the list of terms to display.
1448 * @param array $term_list List of terms to display.
1449 * @param string $taxonomy The taxonomy name.
1450 * @param string $before String to use before the terms.
1451 * @param string $sep String to use between the terms.
1452 * @param string $after String to use after the terms.
1454 echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
1458 * Check if the current post has any of given category.
1462 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1463 * @param int|object $post Optional. Post to check instead of the current post.
1464 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1466 function has_category( $category = '', $post = null ) {
1467 return has_term( $category, 'category', $post );
1471 * Check if the current post has any of given tags.
1473 * The given tags are checked against the post's tags' term_ids, names and slugs.
1474 * Tags given as integers will only be checked against the post's tags' term_ids.
1475 * If no tags are given, determines if post has any tags.
1477 * 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)
1478 * Prior to v2.7, this function could only be used in the WordPress Loop.
1479 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1483 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1484 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1485 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1487 function has_tag( $tag = '', $post = null ) {
1488 return has_term( $tag, 'post_tag', $post );
1492 * Check if the current post has any of given terms.
1494 * The given terms are checked against the post's terms' term_ids, names and slugs.
1495 * Terms given as integers will only be checked against the post's terms' term_ids.
1496 * If no terms are given, determines if post has any terms.
1500 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1501 * @param string $taxonomy Taxonomy name
1502 * @param int|object $post Optional. Post to check instead of the current post.
1503 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1505 function has_term( $term = '', $taxonomy = '', $post = null ) {
1506 $post = get_post($post);
1511 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1512 if ( is_wp_error( $r ) )