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 ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
61 $chain .= $name.$separator;
66 * Retrieve post categories.
71 * @param int $id Optional, default to current post ID. The post ID.
74 function get_the_category( $id = false ) {
75 $categories = get_the_terms( $id, 'category' );
76 if ( ! $categories || is_wp_error( $categories ) )
77 $categories = array();
79 $categories = array_values( $categories );
81 foreach ( array_keys( $categories ) as $key ) {
82 _make_cat_compat( $categories[$key] );
85 // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
86 return apply_filters( 'get_the_categories', $categories );
90 * Sort categories by name.
92 * Used by usort() as a callback, should not be used directly. Can actually be
93 * used to sort any term object.
102 function _usort_terms_by_name( $a, $b ) {
103 return strcmp( $a->name, $b->name );
107 * Sort categories by ID.
109 * Used by usort() as a callback, should not be used directly. Can actually be
110 * used to sort any term object.
119 function _usort_terms_by_ID( $a, $b ) {
120 if ( $a->term_id > $b->term_id )
122 elseif ( $a->term_id < $b->term_id )
129 * Retrieve category name based on category ID.
133 * @param int $cat_ID Category ID.
134 * @return string|WP_Error Category name on success, WP_Error on failure.
136 function get_the_category_by_ID( $cat_ID ) {
137 $cat_ID = (int) $cat_ID;
138 $category = get_term( $cat_ID, 'category' );
139 if ( is_wp_error( $category ) )
141 return $category->name;
145 * Retrieve category list in either HTML list or custom format.
149 * @param string $separator Optional, default is empty string. Separator for between the categories.
150 * @param string $parents Optional. How to display the parents.
151 * @param int $post_id Optional. Post ID to retrieve categories.
154 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
156 if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
157 return apply_filters( 'the_category', '', $separator, $parents );
159 $categories = get_the_category( $post_id );
160 if ( empty( $categories ) )
161 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
163 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
166 if ( '' == $separator ) {
167 $thelist .= '<ul class="post-categories">';
168 foreach ( $categories as $category ) {
169 $thelist .= "\n\t<li>";
170 switch ( strtolower( $parents ) ) {
172 if ( $category->parent )
173 $thelist .= get_category_parents( $category->parent, true, $separator );
174 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
177 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
178 if ( $category->parent )
179 $thelist .= get_category_parents( $category->parent, false, $separator );
180 $thelist .= $category->name.'</a></li>';
184 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a></li>';
190 foreach ( $categories as $category ) {
192 $thelist .= $separator;
193 switch ( strtolower( $parents ) ) {
195 if ( $category->parent )
196 $thelist .= get_category_parents( $category->parent, true, $separator );
197 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
200 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
201 if ( $category->parent )
202 $thelist .= get_category_parents( $category->parent, false, $separator );
203 $thelist .= "$category->name</a>";
207 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->name.'</a>';
212 return apply_filters( 'the_category', $thelist, $separator, $parents );
216 * Check if the current post in within any of the given categories.
218 * The given categories are checked against the post's categories' term_ids, names and slugs.
219 * Categories given as integers will only be checked against the post's categories' term_ids.
221 * Prior to v2.5 of WordPress, category names were not supported.
222 * Prior to v2.7, category slugs were not supported.
223 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
224 * Prior to v2.7, this function could only be used in the WordPress Loop.
225 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
228 * @uses has_category()
230 * @param int|string|array $category Category ID, name or slug, or array of said.
231 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
232 * @return bool True if the current post is in any of the given categories.
234 function in_category( $category, $post = null ) {
235 if ( empty( $category ) )
238 return has_category( $category, $post );
242 * Display the category list for the post.
246 * @param string $separator Optional, default is empty string. Separator for between the categories.
247 * @param string $parents Optional. How to display the parents.
248 * @param int $post_id Optional. Post ID to retrieve categories.
250 function the_category( $separator = '', $parents='', $post_id = false ) {
251 echo get_the_category_list( $separator, $parents, $post_id );
255 * Retrieve category description.
259 * @param int $category Optional. Category ID. Will use global category ID by default.
260 * @return string Category description, available.
262 function category_description( $category = 0 ) {
263 return term_description( $category, 'category' );
267 * Display or retrieve the HTML dropdown list of categories.
269 * The list of arguments is below:
270 * 'show_option_all' (string) - Text to display for showing all categories.
271 * 'show_option_none' (string) - Text to display for showing no categories.
272 * 'orderby' (string) default is 'ID' - What column to use for ordering the
274 * 'order' (string) default is 'ASC' - What direction to order categories.
275 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
277 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
278 * don't have any posts attached to them.
279 * 'child_of' (int) default is 0 - See {@link get_categories()}.
280 * 'exclude' (string) - See {@link get_categories()}.
281 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
282 * 'depth' (int) - The max depth.
283 * 'tab_index' (int) - Tab index for select element.
284 * 'name' (string) - The name attribute value for select element.
285 * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
286 * 'class' (string) - The class attribute value for select element.
287 * 'selected' (int) - Which category ID is selected.
288 * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
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.
297 * @param string|array $args Optional. Override default arguments.
298 * @return string HTML content only if 'echo' argument is 0.
300 function wp_dropdown_categories( $args = '' ) {
302 'show_option_all' => '', 'show_option_none' => '',
303 'orderby' => 'id', 'order' => 'ASC',
305 'hide_empty' => 1, 'child_of' => 0,
306 'exclude' => '', 'echo' => 1,
307 'selected' => 0, 'hierarchical' => 0,
308 'name' => 'cat', 'id' => '',
309 'class' => 'postform', 'depth' => 0,
310 'tab_index' => 0, 'taxonomy' => 'category',
311 'hide_if_empty' => false
314 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
317 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
318 _deprecated_argument( __FUNCTION__, '3.0', '' );
319 $args['taxonomy'] = 'link_category';
322 $r = wp_parse_args( $args, $defaults );
324 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
325 $r['pad_counts'] = true;
330 $tab_index_attribute = '';
331 if ( (int) $tab_index > 0 )
332 $tab_index_attribute = " tabindex=\"$tab_index\"";
334 $categories = get_terms( $taxonomy, $r );
335 $name = esc_attr( $name );
336 $class = esc_attr( $class );
337 $id = $id ? esc_attr( $id ) : $name;
339 if ( ! $r['hide_if_empty'] || ! empty($categories) )
340 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
344 if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
345 $show_option_none = apply_filters( 'list_cats', $show_option_none );
346 $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
349 if ( ! empty( $categories ) ) {
351 if ( $show_option_all ) {
352 $show_option_all = apply_filters( 'list_cats', $show_option_all );
353 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
354 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
357 if ( $show_option_none ) {
358 $show_option_none = apply_filters( 'list_cats', $show_option_none );
359 $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
360 $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
364 $depth = $r['depth']; // Walk the full depth.
366 $depth = -1; // Flat.
368 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
371 if ( ! $r['hide_if_empty'] || ! empty($categories) )
372 $output .= "</select>\n";
374 $output = apply_filters( 'wp_dropdown_cats', $output );
383 * Display or retrieve the HTML list of categories.
385 * The list of arguments is below:
386 * 'show_option_all' (string) - Text to display for showing all categories.
387 * 'orderby' (string) default is 'ID' - What column to use for ordering the
389 * 'order' (string) default is 'ASC' - What direction to order categories.
390 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
392 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
393 * don't have any posts attached to them.
394 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
395 * description instead of the category title.
396 * 'feed' - See {@link get_categories()}.
397 * 'feed_type' - See {@link get_categories()}.
398 * 'feed_image' - See {@link get_categories()}.
399 * 'child_of' (int) default is 0 - See {@link get_categories()}.
400 * 'exclude' (string) - See {@link get_categories()}.
401 * 'exclude_tree' (string) - See {@link get_categories()}.
402 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
403 * 'current_category' (int) - See {@link get_categories()}.
404 * 'hierarchical' (bool) - See {@link get_categories()}.
405 * 'title_li' (string) - See {@link get_categories()}.
406 * 'depth' (int) - The max depth.
410 * @param string|array $args Optional. Override default arguments.
411 * @return string HTML content only if 'echo' argument is 0.
413 function wp_list_categories( $args = '' ) {
415 'show_option_all' => '', 'show_option_none' => __('No categories'),
416 'orderby' => 'name', 'order' => 'ASC',
418 'show_count' => 0, 'hide_empty' => 1,
419 'use_desc_for_title' => 1, 'child_of' => 0,
420 'feed' => '', 'feed_type' => '',
421 'feed_image' => '', 'exclude' => '',
422 'exclude_tree' => '', 'current_category' => 0,
423 'hierarchical' => true, 'title_li' => __( 'Categories' ),
424 'echo' => 1, 'depth' => 0,
425 'taxonomy' => 'category'
428 $r = wp_parse_args( $args, $defaults );
430 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
431 $r['pad_counts'] = true;
433 if ( true == $r['hierarchical'] ) {
434 $r['exclude_tree'] = $r['exclude'];
438 if ( !isset( $r['class'] ) )
439 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
443 if ( !taxonomy_exists($taxonomy) )
446 $categories = get_categories( $r );
449 if ( $title_li && 'list' == $style )
450 $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
452 if ( empty( $categories ) ) {
453 if ( ! empty( $show_option_none ) ) {
454 if ( 'list' == $style )
455 $output .= '<li class="cat-item-none">' . $show_option_none . '</li>';
457 $output .= $show_option_none;
460 if ( ! empty( $show_option_all ) ) {
461 $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
462 $posts_page = esc_url( $posts_page );
463 if ( 'list' == $style )
464 $output .= "<li class='cat-item-all'><a href='$posts_page'>$show_option_all</a></li>";
466 $output .= "<a href='$posts_page'>$show_option_all</a>";
469 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
470 $current_term_object = get_queried_object();
471 if ( $current_term_object && $r['taxonomy'] === $current_term_object->taxonomy )
472 $r['current_category'] = get_queried_object_id();
476 $depth = $r['depth'];
478 $depth = -1; // Flat.
480 $output .= walk_category_tree( $categories, $depth, $r );
483 if ( $title_li && 'list' == $style )
484 $output .= '</ul></li>';
486 $output = apply_filters( 'wp_list_categories', $output, $args );
497 * The text size is set by the 'smallest' and 'largest' arguments, which will
498 * use the 'unit' argument value for the CSS text size unit. The 'format'
499 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
500 * 'format' argument will separate tags with spaces. The list value for the
501 * 'format' argument will format the tags in a UL HTML list. The array value for
502 * the 'format' argument will return in PHP array type format.
504 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
505 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
507 * The 'number' argument is how many tags to return. By default, the limit will
508 * be to return the top 45 tags in the tag cloud list.
510 * The 'topic_count_text_callback' argument is a function, which, given the count
511 * of the posts with that tag, returns a text for the tooltip of the tag link.
513 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
514 * function. Only one should be used, because only one will be used and the
515 * other ignored, if they are both set.
519 * @param array|string $args Optional. Override default arguments.
520 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
522 function wp_tag_cloud( $args = '' ) {
524 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
525 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
526 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
528 $args = wp_parse_args( $args, $defaults );
530 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
532 if ( empty( $tags ) || is_wp_error( $tags ) )
535 foreach ( $tags as $key => $tag ) {
536 if ( 'edit' == $args['link'] )
537 $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
539 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
540 if ( is_wp_error( $link ) )
543 $tags[ $key ]->link = $link;
544 $tags[ $key ]->id = $tag->term_id;
547 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
549 $return = apply_filters( 'wp_tag_cloud', $return, $args );
551 if ( 'array' == $args['format'] || empty($args['echo']) )
558 * Default text for tooltip for tag links
560 * @param integer $count number of posts with that tag
561 * @return string text for the tooltip of a tag link.
563 function default_topic_count_text( $count ) {
564 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
568 * Default topic count scaling for tag links
570 * @param integer $count number of posts with that tag
571 * @return integer scaled count
573 function default_topic_count_scale( $count ) {
574 return round(log10($count + 1) * 100);
578 * Generates a tag cloud (heatmap) from provided data.
580 * The text size is set by the 'smallest' and 'largest' arguments, which will
581 * use the 'unit' argument value for the CSS text size unit. The 'format'
582 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
583 * 'format' argument will separate tags with spaces. The list value for the
584 * 'format' argument will format the tags in a UL HTML list. The array value for
585 * the 'format' argument will return in PHP array type format.
587 * The 'tag_cloud_sort' filter allows you to override the sorting.
588 * Passed to the filter: $tags array and $args array, has to return the $tags array
591 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
592 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
595 * The 'number' argument is how many tags to return. By default, the limit will
596 * be to return the entire tag cloud list.
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 * @todo Complete functionality.
604 * @param array $tags List of tags.
605 * @param string|array $args Optional, override default arguments.
608 function wp_generate_tag_cloud( $tags, $args = '' ) {
610 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
611 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
612 'topic_count_text_callback' => 'default_topic_count_text',
613 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
616 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
617 $body = 'return sprintf (
618 _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
619 number_format_i18n( $count ));';
620 $args['topic_count_text_callback'] = create_function('$count', $body);
623 $args = wp_parse_args( $args, $defaults );
626 if ( empty( $tags ) )
629 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
630 if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
631 $tags = $tags_sorted;
634 if ( 'RAND' == $order ) {
637 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
638 if ( 'name' == $orderby )
639 uasort( $tags, '_wp_object_name_sort_cb' );
641 uasort( $tags, '_wp_object_count_sort_cb' );
643 if ( 'DESC' == $order )
644 $tags = array_reverse( $tags, true );
649 $tags = array_slice($tags, 0, $number);
652 $real_counts = array(); // For the alt tag
653 foreach ( (array) $tags as $key => $tag ) {
654 $real_counts[ $key ] = $tag->count;
655 $counts[ $key ] = $topic_count_scale_callback($tag->count);
658 $min_count = min( $counts );
659 $spread = max( $counts ) - $min_count;
662 $font_spread = $largest - $smallest;
663 if ( $font_spread < 0 )
665 $font_step = $font_spread / $spread;
669 foreach ( $tags as $key => $tag ) {
670 $count = $counts[ $key ];
671 $real_count = $real_counts[ $key ];
672 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
673 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
674 $tag_name = $tags[ $key ]->name;
675 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count, $tag, $args ) ) . "' style='font-size: " .
676 str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
677 . "$unit;'>$tag_name</a>";
685 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
686 $return .= join( "</li>\n\t<li>", $a );
687 $return .= "</li>\n</ul>\n";
690 $return = join( $separator, $a );
695 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
701 * Callback for comparing objects based on name
706 function _wp_object_name_sort_cb( $a, $b ) {
707 return strnatcasecmp( $a->name, $b->name );
711 * Callback for comparing objects based on count
716 function _wp_object_count_sort_cb( $a, $b ) {
717 return ( $a->count > $b->count );
725 * Retrieve HTML list content for category list.
727 * @uses Walker_Category to create HTML list content.
729 * @see Walker_Category::walk() for parameters and return description.
731 function walk_category_tree() {
732 $args = func_get_args();
733 // the user's options are the third parameter
734 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
735 $walker = new Walker_Category;
737 $walker = $args[2]['walker'];
739 return call_user_func_array(array( &$walker, 'walk' ), $args );
743 * Retrieve HTML dropdown (select) content for category list.
745 * @uses Walker_CategoryDropdown to create HTML dropdown content.
747 * @see Walker_CategoryDropdown::walk() for parameters and return description.
749 function walk_category_dropdown_tree() {
750 $args = func_get_args();
751 // the user's options are the third parameter
752 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
753 $walker = new Walker_CategoryDropdown;
755 $walker = $args[2]['walker'];
757 return call_user_func_array(array( &$walker, 'walk' ), $args );
761 * Create HTML list of categories.
767 class Walker_Category extends Walker {
769 * What the class handles.
771 * @see Walker::$tree_type
775 var $tree_type = 'category';
778 * Database fields to use.
780 * @see Walker::$db_fields
782 * @todo Decouple this
785 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
788 * Starts the list before the elements are added.
790 * @see Walker::start_lvl()
794 * @param string $output Passed by reference. Used to append additional content.
795 * @param int $depth Depth of category. Used for tab indentation.
796 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
797 * @see wp_list_categories()
799 function start_lvl( &$output, $depth = 0, $args = array() ) {
800 if ( 'list' != $args['style'] )
803 $indent = str_repeat("\t", $depth);
804 $output .= "$indent<ul class='children'>\n";
808 * Ends the list of after the elements are added.
810 * @see Walker::end_lvl()
814 * @param string $output Passed by reference. Used to append additional content.
815 * @param int $depth Depth of category. Used for tab indentation.
816 * @param array $args An array of arguments. Will only append content if style argument value is 'list'.
817 * @wsee wp_list_categories()
819 function end_lvl( &$output, $depth = 0, $args = array() ) {
820 if ( 'list' != $args['style'] )
823 $indent = str_repeat("\t", $depth);
824 $output .= "$indent</ul>\n";
828 * Start the element output.
830 * @see Walker::start_el()
834 * @param string $output Passed by reference. Used to append additional content.
835 * @param object $category Category data object.
836 * @param int $depth Depth of category in reference to parents. Default 0.
837 * @param array $args An array of arguments. @see wp_list_categories()
838 * @param int $id ID of the current category.
840 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
843 $cat_name = esc_attr( $category->name );
844 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
845 $link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
846 if ( $use_desc_for_title == 0 || empty($category->description) )
847 $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
849 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
851 $link .= $cat_name . '</a>';
853 if ( !empty($feed_image) || !empty($feed) ) {
856 if ( empty($feed_image) )
859 $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
861 if ( empty($feed) ) {
862 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
864 $title = ' title="' . $feed . '"';
865 $alt = ' alt="' . $feed . '"';
872 if ( empty($feed_image) )
875 $link .= "<img src='$feed_image'$alt$title" . ' />';
879 if ( empty($feed_image) )
883 if ( !empty($show_count) )
884 $link .= ' (' . intval($category->count) . ')';
886 if ( 'list' == $args['style'] ) {
888 $class = 'cat-item cat-item-' . $category->term_id;
889 if ( !empty($current_category) ) {
890 $_current_category = get_term( $current_category, $category->taxonomy );
891 if ( $category->term_id == $current_category )
892 $class .= ' current-cat';
893 elseif ( $category->term_id == $_current_category->parent )
894 $class .= ' current-cat-parent';
896 $output .= ' class="' . $class . '"';
897 $output .= ">$link\n";
899 $output .= "\t$link<br />\n";
904 * Ends the element output, if needed.
906 * @see Walker::end_el()
910 * @param string $output Passed by reference. Used to append additional content.
911 * @param object $page Not used.
912 * @param int $depth Depth of category. Not used.
913 * @param array $args An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
915 function end_el( &$output, $page, $depth = 0, $args = array() ) {
916 if ( 'list' != $args['style'] )
919 $output .= "</li>\n";
925 * Create HTML dropdown list of Categories.
931 class Walker_CategoryDropdown extends Walker {
933 * @see Walker::$tree_type
937 var $tree_type = 'category';
940 * @see Walker::$db_fields
942 * @todo Decouple this
945 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
948 * Start the element output.
950 * @see Walker::start_el()
953 * @param string $output Passed by reference. Used to append additional content.
954 * @param object $category Category data object.
955 * @param int $depth Depth of category. Used for padding.
956 * @param array $args Uses 'selected' and 'show_count' keys, if they exist. @see wp_dropdown_categories()
958 function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
959 $pad = str_repeat(' ', $depth * 3);
961 $cat_name = apply_filters('list_cats', $category->name, $category);
962 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
963 if ( $category->term_id == $args['selected'] )
964 $output .= ' selected="selected"';
966 $output .= $pad.$cat_name;
967 if ( $args['show_count'] )
968 $output .= ' ('. $category->count .')';
969 $output .= "</option>\n";
978 * Retrieve the link to the tag.
981 * @see get_term_link()
983 * @param int|object $tag Tag ID or object.
984 * @return string Link on success, empty string if tag does not exist.
986 function get_tag_link( $tag ) {
987 if ( ! is_object( $tag ) )
990 $tag = get_term_link( $tag, 'post_tag' );
992 if ( is_wp_error( $tag ) )
999 * Retrieve the tags for a post.
1002 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
1004 * @param int $id Post ID.
1005 * @return array|bool Array of tag objects on success, false on failure.
1007 function get_the_tags( $id = 0 ) {
1008 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1012 * Retrieve the tags for a post formatted as a string.
1015 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
1017 * @param string $before Optional. Before tags.
1018 * @param string $sep Optional. Between tags.
1019 * @param string $after Optional. After tags.
1020 * @param int $id Optional. Post ID. Defaults to the current post.
1021 * @return string|bool|WP_Error A list of tags on success, false or WP_Error on failure.
1023 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
1024 return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
1028 * Retrieve the tags for a post.
1032 * @param string $before Optional. Before list.
1033 * @param string $sep Optional. Separate items using this.
1034 * @param string $after Optional. After list.
1036 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1037 if ( null === $before )
1038 $before = __('Tags: ');
1039 echo get_the_tag_list($before, $sep, $after);
1043 * Retrieve tag description.
1047 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1048 * @return string Tag description, available.
1050 function tag_description( $tag = 0 ) {
1051 return term_description( $tag );
1055 * Retrieve term description.
1059 * @param int $term Optional. Term ID. Will use global term ID by default.
1060 * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
1061 * @return string Term description, available.
1063 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1064 if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
1065 $term = get_queried_object();
1067 $taxonomy = $term->taxonomy;
1068 $term = $term->term_id;
1071 $description = get_term_field( 'description', $term, $taxonomy );
1072 return is_wp_error( $description ) ? '' : $description;
1076 * Retrieve the terms of the taxonomy that are attached to the post.
1080 * @param int|object $post Post ID or object.
1081 * @param string $taxonomy Taxonomy name.
1082 * @return array|bool|WP_Error Array of term objects on success, false or WP_Error on failure.
1084 function get_the_terms( $post, $taxonomy ) {
1085 if ( ! $post = get_post( $post ) )
1088 $terms = get_object_term_cache( $post->ID, $taxonomy );
1089 if ( false === $terms ) {
1090 $terms = wp_get_object_terms( $post->ID, $taxonomy );
1091 wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
1094 $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
1096 if ( empty( $terms ) )
1103 * Retrieve a post's terms as a list with specified format.
1107 * @param int $id Post ID.
1108 * @param string $taxonomy Taxonomy name.
1109 * @param string $before Optional. Before list.
1110 * @param string $sep Optional. Separate items using this.
1111 * @param string $after Optional. After list.
1112 * @return string|bool|WP_Error A list of terms on success, false or WP_Error on failure.
1114 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
1115 $terms = get_the_terms( $id, $taxonomy );
1117 if ( is_wp_error( $terms ) )
1120 if ( empty( $terms ) )
1123 foreach ( $terms as $term ) {
1124 $link = get_term_link( $term, $taxonomy );
1125 if ( is_wp_error( $link ) )
1127 $term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
1130 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
1132 return $before . join( $sep, $term_links ) . $after;
1136 * Display the terms in a list.
1140 * @param int $id Post ID.
1141 * @param string $taxonomy Taxonomy name.
1142 * @param string $before Optional. Before list.
1143 * @param string $sep Optional. Separate items using this.
1144 * @param string $after Optional. After list.
1145 * @return null|bool False on WordPress error. Returns null when displaying.
1147 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
1148 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
1150 if ( is_wp_error( $term_list ) )
1153 echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
1157 * Check if the current post has any of given category.
1161 * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
1162 * @param int|object $post Optional. Post to check instead of the current post.
1163 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1165 function has_category( $category = '', $post = null ) {
1166 return has_term( $category, 'category', $post );
1170 * Check if the current post has any of given tags.
1172 * The given tags are checked against the post's tags' term_ids, names and slugs.
1173 * Tags given as integers will only be checked against the post's tags' term_ids.
1174 * If no tags are given, determines if post has any tags.
1176 * 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)
1177 * Prior to v2.7, this function could only be used in the WordPress Loop.
1178 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1182 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1183 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1184 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1186 function has_tag( $tag = '', $post = null ) {
1187 return has_term( $tag, 'post_tag', $post );
1191 * Check if the current post has any of given terms.
1193 * The given terms are checked against the post's terms' term_ids, names and slugs.
1194 * Terms given as integers will only be checked against the post's terms' term_ids.
1195 * If no terms are given, determines if post has any terms.
1199 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1200 * @param string $taxonomy Taxonomy name
1201 * @param int|object $post Optional. Post to check instead of the current post.
1202 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1204 function has_term( $term = '', $taxonomy = '', $post = null ) {
1205 $post = get_post($post);
1210 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1211 if ( is_wp_error( $r ) )