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.
42 function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
44 $parent = &get_category( $id );
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="' . 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' );
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 Category name.
136 function get_the_category_by_ID( $cat_ID ) {
137 $cat_ID = (int) $cat_ID;
138 $category = &get_category( $cat_ID );
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 $categories = get_the_category( $post_id );
157 if ( !is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
158 return apply_filters( 'the_category', '', $separator, $parents );
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="' . 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="' . 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="' . 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="' . 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="' . 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="' . 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 );
217 * Check if the current post in within any of the given categories.
219 * The given categories are checked against the post's categories' term_ids, names and slugs.
220 * Categories given as integers will only be checked against the post's categories' term_ids.
222 * Prior to v2.5 of WordPress, category names were not supported.
223 * Prior to v2.7, category slugs were not supported.
224 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
225 * Prior to v2.7, this function could only be used in the WordPress Loop.
226 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
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_term( $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_last_update' (bool|int) default is 0 - See {@link get_categories()}
276 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
278 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
279 * don't have any posts attached to them.
280 * 'child_of' (int) default is 0 - See {@link get_categories()}.
281 * 'exclude' (string) - See {@link get_categories()}.
282 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
283 * 'depth' (int) - The max depth.
284 * 'tab_index' (int) - Tab index for select element.
285 * 'name' (string) - The name attribute value for select element.
286 * 'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
287 * 'class' (string) - The class attribute value for select element.
288 * 'selected' (int) - Which category ID is selected.
289 * 'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
291 * The 'hierarchical' argument, which is disabled by default, will override the
292 * depth argument, unless it is true. When the argument is false, it will
293 * display all of the categories. When it is enabled it will use the value in
294 * the 'depth' argument.
298 * @param string|array $args Optional. Override default arguments.
299 * @return string HTML content only if 'echo' argument is 0.
301 function wp_dropdown_categories( $args = '' ) {
303 'show_option_all' => '', 'show_option_none' => '',
304 'orderby' => 'id', 'order' => 'ASC',
305 'show_last_update' => 0, 'show_count' => 0,
306 'hide_empty' => 1, 'child_of' => 0,
307 'exclude' => '', 'echo' => 1,
308 'selected' => 0, 'hierarchical' => 0,
309 'name' => 'cat', 'id' => '',
310 'class' => 'postform', 'depth' => 0,
311 'tab_index' => 0, 'taxonomy' => 'category',
312 'hide_if_empty' => false
315 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
318 if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
319 _deprecated_argument( __FUNCTION__, '3.0', '' );
320 $args['taxonomy'] = 'link_category';
323 $r = wp_parse_args( $args, $defaults );
325 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
326 $r['pad_counts'] = true;
329 $r['include_last_update_time'] = $r['show_last_update'];
332 $tab_index_attribute = '';
333 if ( (int) $tab_index > 0 )
334 $tab_index_attribute = " tabindex=\"$tab_index\"";
336 $categories = get_terms( $taxonomy, $r );
337 $name = esc_attr( $name );
338 $class = esc_attr( $class );
339 $id = $id ? esc_attr( $id ) : $name;
341 if ( ! $r['hide_if_empty'] || ! empty($categories) )
342 $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
346 if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
347 $show_option_none = apply_filters( 'list_cats', $show_option_none );
348 $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
351 if ( ! empty( $categories ) ) {
353 if ( $show_option_all ) {
354 $show_option_all = apply_filters( 'list_cats', $show_option_all );
355 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
356 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
359 if ( $show_option_none ) {
360 $show_option_none = apply_filters( 'list_cats', $show_option_none );
361 $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
362 $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
366 $depth = $r['depth']; // Walk the full depth.
368 $depth = -1; // Flat.
370 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
372 if ( ! $r['hide_if_empty'] || ! empty($categories) )
373 $output .= "</select>\n";
376 $output = apply_filters( 'wp_dropdown_cats', $output );
385 * Display or retrieve the HTML list of categories.
387 * The list of arguments is below:
388 * 'show_option_all' (string) - Text to display for showing all categories.
389 * 'orderby' (string) default is 'ID' - What column to use for ordering the
391 * 'order' (string) default is 'ASC' - What direction to order categories.
392 * 'show_last_update' (bool|int) default is 0 - See {@link
393 * walk_category_dropdown_tree()}
394 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
396 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
397 * don't have any posts attached to them.
398 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
399 * description instead of the category title.
400 * 'feed' - See {@link get_categories()}.
401 * 'feed_type' - See {@link get_categories()}.
402 * 'feed_image' - See {@link get_categories()}.
403 * 'child_of' (int) default is 0 - See {@link get_categories()}.
404 * 'exclude' (string) - See {@link get_categories()}.
405 * 'exclude_tree' (string) - See {@link get_categories()}.
406 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
407 * 'current_category' (int) - See {@link get_categories()}.
408 * 'hierarchical' (bool) - See {@link get_categories()}.
409 * 'title_li' (string) - See {@link get_categories()}.
410 * 'depth' (int) - The max depth.
414 * @param string|array $args Optional. Override default arguments.
415 * @return string HTML content only if 'echo' argument is 0.
417 function wp_list_categories( $args = '' ) {
419 'show_option_all' => '', 'show_option_none' => __('No categories'),
420 'orderby' => 'name', 'order' => 'ASC',
421 'show_last_update' => 0, 'style' => 'list',
422 'show_count' => 0, 'hide_empty' => 1,
423 'use_desc_for_title' => 1, 'child_of' => 0,
424 'feed' => '', 'feed_type' => '',
425 'feed_image' => '', 'exclude' => '',
426 'exclude_tree' => '', 'current_category' => 0,
427 'hierarchical' => true, 'title_li' => __( 'Categories' ),
428 'echo' => 1, 'depth' => 0,
429 'taxonomy' => 'category'
432 $r = wp_parse_args( $args, $defaults );
434 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
435 $r['pad_counts'] = true;
437 if ( isset( $r['show_date'] ) )
438 $r['include_last_update_time'] = $r['show_date'];
440 if ( true == $r['hierarchical'] ) {
441 $r['exclude_tree'] = $r['exclude'];
445 if ( !isset( $r['class'] ) )
446 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
450 if ( !taxonomy_exists($taxonomy) )
453 $categories = get_categories( $r );
456 if ( $title_li && 'list' == $style )
457 $output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
459 if ( empty( $categories ) ) {
460 if ( ! empty( $show_option_none ) ) {
461 if ( 'list' == $style )
462 $output .= '<li>' . $show_option_none . '</li>';
464 $output .= $show_option_none;
467 if ( ! empty( $show_option_all ) ) {
468 $posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
469 $posts_page = esc_url( $posts_page );
470 if ( 'list' == $style )
471 $output .= "<li><a href='$posts_page'>$show_option_all</a></li>";
473 $output .= "<a href='$posts_page'>$show_option_all</a>";
476 if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
477 $current_term_object = get_queried_object();
478 if ( $r['taxonomy'] == $current_term_object->taxonomy )
479 $r['current_category'] = get_queried_object_id();
483 $depth = $r['depth'];
485 $depth = -1; // Flat.
487 $output .= walk_category_tree( $categories, $depth, $r );
490 if ( $title_li && 'list' == $style )
491 $output .= '</ul></li>';
493 $output = apply_filters( 'wp_list_categories', $output, $args );
504 * The text size is set by the 'smallest' and 'largest' arguments, which will
505 * use the 'unit' argument value for the CSS text size unit. The 'format'
506 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
507 * 'format' argument will separate tags with spaces. The list value for the
508 * 'format' argument will format the tags in a UL HTML list. The array value for
509 * the 'format' argument will return in PHP array type format.
511 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
512 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
514 * The 'number' argument is how many tags to return. By default, the limit will
515 * be to return the top 45 tags in the tag cloud list.
517 * The 'topic_count_text_callback' argument is a function, which, given the count
518 * of the posts with that tag, returns a text for the tooltip of the tag link.
520 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
521 * function. Only one should be used, because only one will be used and the
522 * other ignored, if they are both set.
526 * @param array|string $args Optional. Override default arguments.
527 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
529 function wp_tag_cloud( $args = '' ) {
531 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
532 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
533 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
535 $args = wp_parse_args( $args, $defaults );
537 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
539 if ( empty( $tags ) || is_wp_error( $tags ) )
542 foreach ( $tags as $key => $tag ) {
543 if ( 'edit' == $args['link'] )
544 $link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
546 $link = get_term_link( intval($tag->term_id), $tag->taxonomy );
547 if ( is_wp_error( $link ) )
550 $tags[ $key ]->link = $link;
551 $tags[ $key ]->id = $tag->term_id;
554 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
556 $return = apply_filters( 'wp_tag_cloud', $return, $args );
558 if ( 'array' == $args['format'] || empty($args['echo']) )
565 * Default text for tooltip for tag links
567 * @param integer $count number of posts with that tag
568 * @return string text for the tooltip of a tag link.
570 function default_topic_count_text( $count ) {
571 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
575 * Default topic count scaling for tag links
577 * @param integer $count number of posts with that tag
578 * @return integer scaled count
580 function default_topic_count_scale( $count ) {
581 return round(log10($count + 1) * 100);
586 * Generates a tag cloud (heatmap) from provided data.
588 * The text size is set by the 'smallest' and 'largest' arguments, which will
589 * use the 'unit' argument value for the CSS text size unit. The 'format'
590 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
591 * 'format' argument will separate tags with spaces. The list value for the
592 * 'format' argument will format the tags in a UL HTML list. The array value for
593 * the 'format' argument will return in PHP array type format.
595 * The 'tag_cloud_sort' filter allows you to override the sorting.
596 * Passed to the filter: $tags array and $args array, has to return the $tags array
599 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
600 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
603 * The 'number' argument is how many tags to return. By default, the limit will
604 * be to return the entire tag cloud list.
606 * The 'topic_count_text_callback' argument is a function, which given the count
607 * of the posts with that tag returns a text for the tooltip of the tag link.
609 * @todo Complete functionality.
612 * @param array $tags List of tags.
613 * @param string|array $args Optional, override default arguments.
616 function wp_generate_tag_cloud( $tags, $args = '' ) {
619 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
620 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
621 'topic_count_text_callback' => 'default_topic_count_text',
622 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
625 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
626 $body = 'return sprintf (
627 _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
628 number_format_i18n( $count ));';
629 $args['topic_count_text_callback'] = create_function('$count', $body);
632 $args = wp_parse_args( $args, $defaults );
635 if ( empty( $tags ) )
638 $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
639 if ( $tags_sorted != $tags ) { // the tags have been sorted by a plugin
640 $tags = $tags_sorted;
643 if ( 'RAND' == $order ) {
646 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
647 if ( 'name' == $orderby )
648 uasort( $tags, '_wp_object_name_sort_cb' );
650 uasort( $tags, '_wp_object_count_sort_cb' );
652 if ( 'DESC' == $order )
653 $tags = array_reverse( $tags, true );
658 $tags = array_slice($tags, 0, $number);
661 $real_counts = array(); // For the alt tag
662 foreach ( (array) $tags as $key => $tag ) {
663 $real_counts[ $key ] = $tag->count;
664 $counts[ $key ] = $topic_count_scale_callback($tag->count);
667 $min_count = min( $counts );
668 $spread = max( $counts ) - $min_count;
671 $font_spread = $largest - $smallest;
672 if ( $font_spread < 0 )
674 $font_step = $font_spread / $spread;
678 foreach ( $tags as $key => $tag ) {
679 $count = $counts[ $key ];
680 $real_count = $real_counts[ $key ];
681 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
682 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
683 $tag_name = $tags[ $key ]->name;
684 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
685 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
686 . "$unit;'>$tag_name</a>";
694 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
695 $return .= join( "</li>\n\t<li>", $a );
696 $return .= "</li>\n</ul>\n";
699 $return = join( $separator, $a );
704 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
710 * Callback for comparing objects based on name
715 function _wp_object_name_sort_cb( $a, $b ) {
716 return strnatcasecmp( $a->name, $b->name );
720 * Callback for comparing objects based on count
725 function _wp_object_count_sort_cb( $a, $b ) {
726 return ( $a->count > $b->count );
734 * Retrieve HTML list content for category list.
736 * @uses Walker_Category to create HTML list content.
738 * @see Walker_Category::walk() for parameters and return description.
740 function walk_category_tree() {
741 $args = func_get_args();
742 // the user's options are the third parameter
743 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
744 $walker = new Walker_Category;
746 $walker = $args[2]['walker'];
748 return call_user_func_array(array( &$walker, 'walk' ), $args );
752 * Retrieve HTML dropdown (select) content for category list.
754 * @uses Walker_CategoryDropdown to create HTML dropdown content.
756 * @see Walker_CategoryDropdown::walk() for parameters and return description.
758 function walk_category_dropdown_tree() {
759 $args = func_get_args();
760 // the user's options are the third parameter
761 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
762 $walker = new Walker_CategoryDropdown;
764 $walker = $args[2]['walker'];
766 return call_user_func_array(array( &$walker, 'walk' ), $args );
770 * Create HTML list of categories.
776 class Walker_Category extends Walker {
778 * @see Walker::$tree_type
782 var $tree_type = 'category';
785 * @see Walker::$db_fields
787 * @todo Decouple this
790 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
793 * @see Walker::start_lvl()
796 * @param string $output Passed by reference. Used to append additional content.
797 * @param int $depth Depth of category. Used for tab indentation.
798 * @param array $args Will only append content if style argument value is 'list'.
800 function start_lvl(&$output, $depth, $args) {
801 if ( 'list' != $args['style'] )
804 $indent = str_repeat("\t", $depth);
805 $output .= "$indent<ul class='children'>\n";
809 * @see Walker::end_lvl()
812 * @param string $output Passed by reference. Used to append additional content.
813 * @param int $depth Depth of category. Used for tab indentation.
814 * @param array $args Will only append content if style argument value is 'list'.
816 function end_lvl(&$output, $depth, $args) {
817 if ( 'list' != $args['style'] )
820 $indent = str_repeat("\t", $depth);
821 $output .= "$indent</ul>\n";
825 * @see Walker::start_el()
828 * @param string $output Passed by reference. Used to append additional content.
829 * @param object $category Category data object.
830 * @param int $depth Depth of category in reference to parents.
833 function start_el(&$output, $category, $depth, $args) {
836 $cat_name = esc_attr( $category->name );
837 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
838 $link = '<a href="' . esc_attr( get_term_link($category) ) . '" ';
839 if ( $use_desc_for_title == 0 || empty($category->description) )
840 $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
842 $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
844 $link .= $cat_name . '</a>';
846 if ( !empty($feed_image) || !empty($feed) ) {
849 if ( empty($feed_image) )
852 $link .= '<a href="' . get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) . '"';
854 if ( empty($feed) ) {
855 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
857 $title = ' title="' . $feed . '"';
858 $alt = ' alt="' . $feed . '"';
865 if ( empty($feed_image) )
868 $link .= "<img src='$feed_image'$alt$title" . ' />';
872 if ( empty($feed_image) )
876 if ( !empty($show_count) )
877 $link .= ' (' . intval($category->count) . ')';
879 if ( !empty($show_date) )
880 $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
882 if ( 'list' == $args['style'] ) {
884 $class = 'cat-item cat-item-' . $category->term_id;
885 if ( !empty($current_category) ) {
886 $_current_category = get_term( $current_category, $category->taxonomy );
887 if ( $category->term_id == $current_category )
888 $class .= ' current-cat';
889 elseif ( $category->term_id == $_current_category->parent )
890 $class .= ' current-cat-parent';
892 $output .= ' class="' . $class . '"';
893 $output .= ">$link\n";
895 $output .= "\t$link<br />\n";
900 * @see Walker::end_el()
903 * @param string $output Passed by reference. Used to append additional content.
904 * @param object $page Not used.
905 * @param int $depth Depth of category. Not used.
906 * @param array $args Only uses 'list' for whether should append to output.
908 function end_el(&$output, $page, $depth, $args) {
909 if ( 'list' != $args['style'] )
912 $output .= "</li>\n";
918 * Create HTML dropdown list of Categories.
924 class Walker_CategoryDropdown extends Walker {
926 * @see Walker::$tree_type
930 var $tree_type = 'category';
933 * @see Walker::$db_fields
935 * @todo Decouple this
938 var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
941 * @see Walker::start_el()
944 * @param string $output Passed by reference. Used to append additional content.
945 * @param object $category Category data object.
946 * @param int $depth Depth of category. Used for padding.
947 * @param array $args Uses 'selected', 'show_count', and 'show_last_update' keys, if they exist.
949 function start_el(&$output, $category, $depth, $args) {
950 $pad = str_repeat(' ', $depth * 3);
952 $cat_name = apply_filters('list_cats', $category->name, $category);
953 $output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
954 if ( $category->term_id == $args['selected'] )
955 $output .= ' selected="selected"';
957 $output .= $pad.$cat_name;
958 if ( $args['show_count'] )
959 $output .= ' ('. $category->count .')';
960 if ( $args['show_last_update'] ) {
962 $output .= ' ' . gmdate($format, $category->last_update_timestamp);
964 $output .= "</option>\n";
973 * Retrieve the link to the tag.
976 * @see get_term_link()
978 * @param int|object $tag Tag ID or object.
979 * @return string Link on success, empty string if tag does not exist.
981 function get_tag_link( $tag ) {
982 if ( ! is_object( $tag ) )
985 $tag = get_term_link( $tag, 'post_tag' );
987 if ( is_wp_error( $tag ) )
994 * Retrieve the tags for a post.
997 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
999 * @param int $id Post ID.
1002 function get_the_tags( $id = 0 ) {
1003 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
1007 * Retrieve the tags for a post formatted as a string.
1010 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
1012 * @param string $before Optional. Before tags.
1013 * @param string $sep Optional. Between tags.
1014 * @param string $after Optional. After tags.
1017 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
1018 return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
1022 * Retrieve the tags for a post.
1026 * @param string $before Optional. Before list.
1027 * @param string $sep Optional. Separate items using this.
1028 * @param string $after Optional. After list.
1031 function the_tags( $before = null, $sep = ', ', $after = '' ) {
1032 if ( null === $before )
1033 $before = __('Tags: ');
1034 echo get_the_tag_list($before, $sep, $after);
1038 * Retrieve tag description.
1042 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
1043 * @return string Tag description, available.
1045 function tag_description( $tag = 0 ) {
1046 return term_description( $tag );
1050 * Retrieve term description.
1054 * @param int $term Optional. Term ID. Will use global term ID by default.
1055 * @return string Term description, available.
1057 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
1058 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
1059 $term = get_queried_object();
1060 $taxonomy = $term->taxonomy;
1061 $term = $term->term_id;
1063 $description = get_term_field( 'description', $term, $taxonomy );
1064 return is_wp_error( $description ) ? '' : $description;
1068 * Retrieve the terms of the taxonomy that are attached to the post.
1072 * @param int $id Post ID. Is not optional.
1073 * @param string $taxonomy Taxonomy name.
1074 * @return array|bool False on failure. Array of term objects on success.
1076 function get_the_terms( $id = 0, $taxonomy ) {
1085 $id = (int) $post->ID;
1088 $terms = get_object_term_cache( $id, $taxonomy );
1089 if ( false === $terms ) {
1090 $terms = wp_get_object_terms( $id, $taxonomy );
1091 wp_cache_add($id, $terms, $taxonomy . '_relationships');
1094 $terms = apply_filters( 'get_the_terms', $terms, $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.
1114 function get_the_term_list( $id = 0, $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="' . $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 = 0, $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);
1158 * Check if the current post has any of given category.
1162 * @param string|int|array $tag Optional. The category name/term_id/slug or array of them to check for.
1163 * @param int|object $post Optional. Post to check instead of the current post.
1164 * @return bool True if the current post has any of the given categories (or any category, if no category specified).
1166 function has_category( $category = '', $post = null ) {
1167 return has_term( $category, 'category', $post );
1171 * Check if the current post has any of given tags.
1173 * The given tags are checked against the post's tags' term_ids, names and slugs.
1174 * Tags given as integers will only be checked against the post's tags' term_ids.
1175 * If no tags are given, determines if post has any tags.
1177 * 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)
1178 * Prior to v2.7, this function could only be used in the WordPress Loop.
1179 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
1183 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
1184 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
1185 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1187 function has_tag( $tag = '', $post = null ) {
1188 return has_term( $tag, 'post_tag', $post );
1192 * Check if the current post has any of given terms.
1194 * The given terms are checked against the post's terms' term_ids, names and slugs.
1195 * Terms given as integers will only be checked against the post's terms' term_ids.
1196 * If no terms are given, determines if post has any terms.
1200 * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
1201 * @param string $taxonomy Taxonomy name
1202 * @param int|object $post Optional. Post to check instead of the current post.
1203 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
1205 function has_term( $term = '', $taxonomy = '', $post = null ) {
1206 $post = get_post($post);
1211 $r = is_object_in_term( $post->ID, $taxonomy, $term );
1212 if ( is_wp_error( $r ) )