web/wp-includes/category-template.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     8 
     8 
     9 /**
     9 /**
    10  * Retrieve category link URL.
    10  * Retrieve category link URL.
    11  *
    11  *
    12  * @since 1.0.0
    12  * @since 1.0.0
    13  * @uses apply_filters() Calls 'category_link' filter on category link and category ID.
    13  * @see get_term_link()
    14  *
    14  *
    15  * @param int $category_id Category ID.
    15  * @param int|object $category Category ID or object.
    16  * @return string
    16  * @return string Link on success, empty string if category does not exist.
    17  */
    17  */
    18 function get_category_link( $category_id ) {
    18 function get_category_link( $category ) {
    19 	global $wp_rewrite;
    19 	if ( ! is_object( $category ) )
    20 	$catlink = $wp_rewrite->get_category_permastruct();
    20 		$category = (int) $category;
    21 
    21 
    22 	if ( empty( $catlink ) ) {
    22 	$category = get_term_link( $category, 'category' );
    23 		$file = get_option( 'home' ) . '/';
    23 
    24 		$catlink = $file . '?cat=' . $category_id;
    24 	if ( is_wp_error( $category ) )
    25 	} else {
    25 		return '';
    26 		$category = &get_category( $category_id );
    26 
    27 		if ( is_wp_error( $category ) )
    27 	return $category;
    28 			return $category;
       
    29 		$category_nicename = $category->slug;
       
    30 
       
    31 		if ( $category->parent == $category_id ) // recursive recursion
       
    32 			$category->parent = 0;
       
    33 		elseif ($category->parent != 0 )
       
    34 			$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
       
    35 
       
    36 		$catlink = str_replace( '%category%', $category_nicename, $catlink );
       
    37 		$catlink = get_option( 'home' ) . user_trailingslashit( $catlink, 'category' );
       
    38 	}
       
    39 	return apply_filters( 'category_link', $catlink, $category_id );
       
    40 }
    28 }
    41 
    29 
    42 /**
    30 /**
    43  * Retrieve category parents with separator.
    31  * Retrieve category parents with separator.
    44  *
    32  *
    58 		return $parent;
    46 		return $parent;
    59 
    47 
    60 	if ( $nicename )
    48 	if ( $nicename )
    61 		$name = $parent->slug;
    49 		$name = $parent->slug;
    62 	else
    50 	else
    63 		$name = $parent->cat_name;
    51 		$name = $parent->name;
    64 
    52 
    65 	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    53 	if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    66 		$visited[] = $parent->parent;
    54 		$visited[] = $parent->parent;
    67 		$chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    55 		$chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
    68 	}
    56 	}
    69 
    57 
    70 	if ( $link )
    58 	if ( $link )
    71 		$chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->cat_name ) ) . '">'.$name.'</a>' . $separator;
    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;
    72 	else
    60 	else
    73 		$chain .= $name.$separator;
    61 		$chain .= $name.$separator;
    74 	return $chain;
    62 	return $chain;
    75 }
    63 }
    76 
    64 
    82  *
    70  *
    83  * @param int $id Optional, default to current post ID. The post ID.
    71  * @param int $id Optional, default to current post ID. The post ID.
    84  * @return array
    72  * @return array
    85  */
    73  */
    86 function get_the_category( $id = false ) {
    74 function get_the_category( $id = false ) {
    87 	global $post;
    75 	$categories = get_the_terms( $id, 'category' );
    88 
    76 	if ( ! $categories )
    89 	$id = (int) $id;
       
    90 	if ( !$id )
       
    91 		$id = (int) $post->ID;
       
    92 
       
    93 	$categories = get_object_term_cache( $id, 'category' );
       
    94 	if ( false === $categories ) {
       
    95 		$categories = wp_get_object_terms( $id, 'category' );
       
    96 		wp_cache_add($id, $categories, 'category_relationships');
       
    97 	}
       
    98 
       
    99 	if ( !empty( $categories ) )
       
   100 		usort( $categories, '_usort_terms_by_name' );
       
   101 	else
       
   102 		$categories = array();
    77 		$categories = array();
   103 
    78 
   104 	foreach ( (array) array_keys( $categories ) as $key ) {
    79 	$categories = array_values( $categories );
       
    80 
       
    81 	foreach ( array_keys( $categories ) as $key ) {
   105 		_make_cat_compat( $categories[$key] );
    82 		_make_cat_compat( $categories[$key] );
   106 	}
    83 	}
   107 
    84 
   108 	return $categories;
    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 );
   109 }
    87 }
   110 
    88 
   111 /**
    89 /**
   112  * Sort categories by name.
    90  * Sort categories by name.
   113  *
    91  *
   173  * @param int $post_id Optional. Post ID to retrieve categories.
   151  * @param int $post_id Optional. Post ID to retrieve categories.
   174  * @return string
   152  * @return string
   175  */
   153  */
   176 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
   154 function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
   177 	global $wp_rewrite;
   155 	global $wp_rewrite;
       
   156 	if ( ! is_object_in_taxonomy( get_post_type( $post_id ), 'category' ) )
       
   157 		return apply_filters( 'the_category', '', $separator, $parents );
       
   158 
   178 	$categories = get_the_category( $post_id );
   159 	$categories = get_the_category( $post_id );
   179 	if ( empty( $categories ) )
   160 	if ( empty( $categories ) )
   180 		return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
   161 		return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
   181 
   162 
   182 	$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
   163 	$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
   188 			$thelist .= "\n\t<li>";
   169 			$thelist .= "\n\t<li>";
   189 			switch ( strtolower( $parents ) ) {
   170 			switch ( strtolower( $parents ) ) {
   190 				case 'multiple':
   171 				case 'multiple':
   191 					if ( $category->parent )
   172 					if ( $category->parent )
   192 						$thelist .= get_category_parents( $category->parent, true, $separator );
   173 						$thelist .= get_category_parents( $category->parent, true, $separator );
   193 					$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>';
   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>';
   194 					break;
   175 					break;
   195 				case 'single':
   176 				case 'single':
   196 					$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
   177 					$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
   197 					if ( $category->parent )
   178 					if ( $category->parent )
   198 						$thelist .= get_category_parents( $category->parent, false, $separator );
   179 						$thelist .= get_category_parents( $category->parent, false, $separator );
   199 					$thelist .= $category->name.'</a></li>';
   180 					$thelist .= $category->name.'</a></li>';
   200 					break;
   181 					break;
   201 				case '':
   182 				case '':
   202 				default:
   183 				default:
   203 					$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->cat_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>';
   204 			}
   185 			}
   205 		}
   186 		}
   206 		$thelist .= '</ul>';
   187 		$thelist .= '</ul>';
   207 	} else {
   188 	} else {
   208 		$i = 0;
   189 		$i = 0;
   209 		foreach ( $categories as $category ) {
   190 		foreach ( $categories as $category ) {
   210 			if ( 0 < $i )
   191 			if ( 0 < $i )
   211 				$thelist .= $separator . ' ';
   192 				$thelist .= $separator;
   212 			switch ( strtolower( $parents ) ) {
   193 			switch ( strtolower( $parents ) ) {
   213 				case 'multiple':
   194 				case 'multiple':
   214 					if ( $category->parent )
   195 					if ( $category->parent )
   215 						$thelist .= get_category_parents( $category->parent, true, $separator );
   196 						$thelist .= get_category_parents( $category->parent, true, $separator );
   216 					$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
   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>';
   217 					break;
   198 					break;
   218 				case 'single':
   199 				case 'single':
   219 					$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
   200 					$thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>';
   220 					if ( $category->parent )
   201 					if ( $category->parent )
   221 						$thelist .= get_category_parents( $category->parent, false, $separator );
   202 						$thelist .= get_category_parents( $category->parent, false, $separator );
   222 					$thelist .= "$category->cat_name</a>";
   203 					$thelist .= "$category->name</a>";
   223 					break;
   204 					break;
   224 				case '':
   205 				case '':
   225 				default:
   206 				default:
   226 					$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '" ' . $rel . '>' . $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>';
   227 			}
   208 			}
   228 			++$i;
   209 			++$i;
   229 		}
   210 		}
   230 	}
   211 	}
   231 	return apply_filters( 'the_category', $thelist, $separator, $parents );
   212 	return apply_filters( 'the_category', $thelist, $separator, $parents );
   232 }
   213 }
   233 
       
   234 
   214 
   235 /**
   215 /**
   236  * Check if the current post in within any of the given categories.
   216  * Check if the current post in within any of the given categories.
   237  *
   217  *
   238  * The given categories are checked against the post's categories' term_ids, names and slugs.
   218  * The given categories are checked against the post's categories' term_ids, names and slugs.
   244  * Prior to v2.7, this function could only be used in the WordPress Loop.
   224  * Prior to v2.7, this function could only be used in the WordPress Loop.
   245  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
   225  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
   246  *
   226  *
   247  * @since 1.2.0
   227  * @since 1.2.0
   248  *
   228  *
   249  * @uses is_object_in_term()
   229  * @param int|string|array $category Category ID, name or slug, or array of said.
   250  *
   230  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
   251  * @param int|string|array $category. Category ID, name or slug, or array of said.
       
   252  * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
       
   253  * @return bool True if the current post is in any of the given categories.
   231  * @return bool True if the current post is in any of the given categories.
   254  */
   232  */
   255 function in_category( $category, $_post = null ) {
   233 function in_category( $category, $post = null ) {
   256 	if ( empty( $category ) )
   234 	if ( empty( $category ) )
   257 		return false;
   235 		return false;
   258 
   236 
   259 	if ( $_post ) {
   237 	return has_term( $category, 'category', $post );
   260 		$_post = get_post( $_post );
       
   261 	} else {
       
   262 		$_post =& $GLOBALS['post'];
       
   263 	}
       
   264 
       
   265 	if ( !$_post )
       
   266 		return false;
       
   267 
       
   268 	$r = is_object_in_term( $_post->ID, 'category', $category );
       
   269 	if ( is_wp_error( $r ) )
       
   270 		return false;
       
   271 	return $r;
       
   272 }
   238 }
   273 
   239 
   274 /**
   240 /**
   275  * Display the category list for the post.
   241  * Display the category list for the post.
   276  *
   242  *
   303  *     'show_option_all' (string) - Text to display for showing all categories.
   269  *     'show_option_all' (string) - Text to display for showing all categories.
   304  *     'show_option_none' (string) - Text to display for showing no categories.
   270  *     'show_option_none' (string) - Text to display for showing no categories.
   305  *     'orderby' (string) default is 'ID' - What column to use for ordering the
   271  *     'orderby' (string) default is 'ID' - What column to use for ordering the
   306  * categories.
   272  * categories.
   307  *     'order' (string) default is 'ASC' - What direction to order categories.
   273  *     'order' (string) default is 'ASC' - What direction to order categories.
   308  *     'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
       
   309  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
   274  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
   310  * in the category.
   275  * in the category.
   311  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
   276  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
   312  * don't have any posts attached to them.
   277  * don't have any posts attached to them.
   313  *     'child_of' (int) default is 0 - See {@link get_categories()}.
   278  *     'child_of' (int) default is 0 - See {@link get_categories()}.
   314  *     'exclude' (string) - See {@link get_categories()}.
   279  *     'exclude' (string) - See {@link get_categories()}.
   315  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
   280  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
   316  *     'depth' (int) - The max depth.
   281  *     'depth' (int) - The max depth.
   317  *     'tab_index' (int) - Tab index for select element.
   282  *     'tab_index' (int) - Tab index for select element.
   318  *     'name' (string) - The name attribute value for selected element.
   283  *     'name' (string) - The name attribute value for select element.
   319  *     'class' (string) - The class attribute value for selected element.
   284  *     'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
       
   285  *     'class' (string) - The class attribute value for select element.
   320  *     'selected' (int) - Which category ID is selected.
   286  *     'selected' (int) - Which category ID is selected.
       
   287  *     'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
   321  *
   288  *
   322  * The 'hierarchical' argument, which is disabled by default, will override the
   289  * The 'hierarchical' argument, which is disabled by default, will override the
   323  * depth argument, unless it is true. When the argument is false, it will
   290  * depth argument, unless it is true. When the argument is false, it will
   324  * display all of the categories. When it is enabled it will use the value in
   291  * display all of the categories. When it is enabled it will use the value in
   325  * the 'depth' argument.
   292  * the 'depth' argument.
   331  */
   298  */
   332 function wp_dropdown_categories( $args = '' ) {
   299 function wp_dropdown_categories( $args = '' ) {
   333 	$defaults = array(
   300 	$defaults = array(
   334 		'show_option_all' => '', 'show_option_none' => '',
   301 		'show_option_all' => '', 'show_option_none' => '',
   335 		'orderby' => 'id', 'order' => 'ASC',
   302 		'orderby' => 'id', 'order' => 'ASC',
   336 		'show_last_update' => 0, 'show_count' => 0,
   303 		'show_count' => 0,
   337 		'hide_empty' => 1, 'child_of' => 0,
   304 		'hide_empty' => 1, 'child_of' => 0,
   338 		'exclude' => '', 'echo' => 1,
   305 		'exclude' => '', 'echo' => 1,
   339 		'selected' => 0, 'hierarchical' => 0,
   306 		'selected' => 0, 'hierarchical' => 0,
   340 		'name' => 'cat', 'class' => 'postform',
   307 		'name' => 'cat', 'id' => '',
   341 		'depth' => 0, 'tab_index' => 0
   308 		'class' => 'postform', 'depth' => 0,
       
   309 		'tab_index' => 0, 'taxonomy' => 'category',
       
   310 		'hide_if_empty' => false
   342 	);
   311 	);
   343 
   312 
   344 	$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
   313 	$defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
       
   314 
       
   315 	// Back compat.
       
   316 	if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
       
   317 		_deprecated_argument( __FUNCTION__, '3.0', '' );
       
   318 		$args['taxonomy'] = 'link_category';
       
   319 	}
   345 
   320 
   346 	$r = wp_parse_args( $args, $defaults );
   321 	$r = wp_parse_args( $args, $defaults );
   347 
   322 
   348 	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
   323 	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
   349 		$r['pad_counts'] = true;
   324 		$r['pad_counts'] = true;
   350 	}
   325 	}
   351 
   326 
   352 	$r['include_last_update_time'] = $r['show_last_update'];
       
   353 	extract( $r );
   327 	extract( $r );
   354 
   328 
   355 	$tab_index_attribute = '';
   329 	$tab_index_attribute = '';
   356 	if ( (int) $tab_index > 0 )
   330 	if ( (int) $tab_index > 0 )
   357 		$tab_index_attribute = " tabindex=\"$tab_index\"";
   331 		$tab_index_attribute = " tabindex=\"$tab_index\"";
   358 
   332 
   359 	$categories = get_categories( $r );
   333 	$categories = get_terms( $taxonomy, $r );
   360 	$name = esc_attr($name);
   334 	$name = esc_attr( $name );
   361 	$class = esc_attr($class);
   335 	$class = esc_attr( $class );
   362 
   336 	$id = $id ? esc_attr( $id ) : $name;
   363 	$output = '';
   337 
       
   338 	if ( ! $r['hide_if_empty'] || ! empty($categories) )
       
   339 		$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
       
   340 	else
       
   341 		$output = '';
       
   342 
       
   343 	if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
       
   344 		$show_option_none = apply_filters( 'list_cats', $show_option_none );
       
   345 		$output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
       
   346 	}
       
   347 
   364 	if ( ! empty( $categories ) ) {
   348 	if ( ! empty( $categories ) ) {
   365 		$output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
       
   366 
   349 
   367 		if ( $show_option_all ) {
   350 		if ( $show_option_all ) {
   368 			$show_option_all = apply_filters( 'list_cats', $show_option_all );
   351 			$show_option_all = apply_filters( 'list_cats', $show_option_all );
   369 			$selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
   352 			$selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
   370 			$output .= "\t<option value='0'$selected>$show_option_all</option>\n";
   353 			$output .= "\t<option value='0'$selected>$show_option_all</option>\n";
   380 			$depth = $r['depth'];  // Walk the full depth.
   363 			$depth = $r['depth'];  // Walk the full depth.
   381 		else
   364 		else
   382 			$depth = -1; // Flat.
   365 			$depth = -1; // Flat.
   383 
   366 
   384 		$output .= walk_category_dropdown_tree( $categories, $depth, $r );
   367 		$output .= walk_category_dropdown_tree( $categories, $depth, $r );
       
   368 	}
       
   369 
       
   370 	if ( ! $r['hide_if_empty'] || ! empty($categories) )
   385 		$output .= "</select>\n";
   371 		$output .= "</select>\n";
   386 	}
       
   387 
   372 
   388 	$output = apply_filters( 'wp_dropdown_cats', $output );
   373 	$output = apply_filters( 'wp_dropdown_cats', $output );
   389 
   374 
   390 	if ( $echo )
   375 	if ( $echo )
   391 		echo $output;
   376 		echo $output;
   399  * The list of arguments is below:
   384  * The list of arguments is below:
   400  *     'show_option_all' (string) - Text to display for showing all categories.
   385  *     'show_option_all' (string) - Text to display for showing all categories.
   401  *     'orderby' (string) default is 'ID' - What column to use for ordering the
   386  *     'orderby' (string) default is 'ID' - What column to use for ordering the
   402  * categories.
   387  * categories.
   403  *     'order' (string) default is 'ASC' - What direction to order categories.
   388  *     'order' (string) default is 'ASC' - What direction to order categories.
   404  *     'show_last_update' (bool|int) default is 0 - See {@link
       
   405  * walk_category_dropdown_tree()}
       
   406  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
   389  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
   407  * in the category.
   390  * in the category.
   408  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
   391  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
   409  * don't have any posts attached to them.
   392  * don't have any posts attached to them.
   410  *     'use_desc_for_title' (bool|int) default is 1 - Whether to use the
   393  *     'use_desc_for_title' (bool|int) default is 1 - Whether to use the
   426  * @param string|array $args Optional. Override default arguments.
   409  * @param string|array $args Optional. Override default arguments.
   427  * @return string HTML content only if 'echo' argument is 0.
   410  * @return string HTML content only if 'echo' argument is 0.
   428  */
   411  */
   429 function wp_list_categories( $args = '' ) {
   412 function wp_list_categories( $args = '' ) {
   430 	$defaults = array(
   413 	$defaults = array(
   431 		'show_option_all' => '', 'orderby' => 'name',
   414 		'show_option_all' => '', 'show_option_none' => __('No categories'),
   432 		'order' => 'ASC', 'show_last_update' => 0,
   415 		'orderby' => 'name', 'order' => 'ASC',
   433 		'style' => 'list', 'show_count' => 0,
   416 		'style' => 'list',
   434 		'hide_empty' => 1, 'use_desc_for_title' => 1,
   417 		'show_count' => 0, 'hide_empty' => 1,
   435 		'child_of' => 0, 'feed' => '', 'feed_type' => '',
   418 		'use_desc_for_title' => 1, 'child_of' => 0,
   436 		'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_category' => 0,
   419 		'feed' => '', 'feed_type' => '',
       
   420 		'feed_image' => '', 'exclude' => '',
       
   421 		'exclude_tree' => '', 'current_category' => 0,
   437 		'hierarchical' => true, 'title_li' => __( 'Categories' ),
   422 		'hierarchical' => true, 'title_li' => __( 'Categories' ),
   438 		'echo' => 1, 'depth' => 0
   423 		'echo' => 1, 'depth' => 0,
       
   424 		'taxonomy' => 'category'
   439 	);
   425 	);
   440 
   426 
   441 	$r = wp_parse_args( $args, $defaults );
   427 	$r = wp_parse_args( $args, $defaults );
   442 
   428 
   443 	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
   429 	if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
   444 		$r['pad_counts'] = true;
   430 		$r['pad_counts'] = true;
   445 	}
       
   446 
       
   447 	if ( isset( $r['show_date'] ) ) {
       
   448 		$r['include_last_update_time'] = $r['show_date'];
       
   449 	}
       
   450 
   431 
   451 	if ( true == $r['hierarchical'] ) {
   432 	if ( true == $r['hierarchical'] ) {
   452 		$r['exclude_tree'] = $r['exclude'];
   433 		$r['exclude_tree'] = $r['exclude'];
   453 		$r['exclude'] = '';
   434 		$r['exclude'] = '';
   454 	}
   435 	}
   455 
   436 
       
   437 	if ( !isset( $r['class'] ) )
       
   438 		$r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
       
   439 
   456 	extract( $r );
   440 	extract( $r );
       
   441 
       
   442 	if ( !taxonomy_exists($taxonomy) )
       
   443 		return false;
   457 
   444 
   458 	$categories = get_categories( $r );
   445 	$categories = get_categories( $r );
   459 
   446 
   460 	$output = '';
   447 	$output = '';
   461 	if ( $title_li && 'list' == $style )
   448 	if ( $title_li && 'list' == $style )
   462 			$output = '<li class="categories">' . $r['title_li'] . '<ul>';
   449 			$output = '<li class="' . esc_attr( $class ) . '">' . $title_li . '<ul>';
   463 
   450 
   464 	if ( empty( $categories ) ) {
   451 	if ( empty( $categories ) ) {
   465 		if ( 'list' == $style )
   452 		if ( ! empty( $show_option_none ) ) {
   466 			$output .= '<li>' . __( "No categories" ) . '</li>';
   453 			if ( 'list' == $style )
   467 		else
   454 				$output .= '<li>' . $show_option_none . '</li>';
   468 			$output .= __( "No categories" );
   455 			else
       
   456 				$output .= $show_option_none;
       
   457 		}
   469 	} else {
   458 	} else {
   470 		global $wp_query;
   459 		if ( ! empty( $show_option_all ) ) {
   471 
   460 			$posts_page = ( 'page' == get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) ) ? get_permalink( get_option( 'page_for_posts' ) ) : home_url( '/' );
   472 		if( !empty( $show_option_all ) )
   461 			$posts_page = esc_url( $posts_page );
   473 			if ( 'list' == $style )
   462 			if ( 'list' == $style )
   474 				$output .= '<li><a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a></li>';
   463 				$output .= "<li><a href='$posts_page'>$show_option_all</a></li>";
   475 			else
   464 			else
   476 				$output .= '<a href="' .  get_bloginfo( 'url' )  . '">' . $show_option_all . '</a>';
   465 				$output .= "<a href='$posts_page'>$show_option_all</a>";
   477 
   466 		}
   478 		if ( empty( $r['current_category'] ) && is_category() )
   467 
   479 			$r['current_category'] = $wp_query->get_queried_object_id();
   468 		if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
       
   469 			$current_term_object = get_queried_object();
       
   470 			if ( $r['taxonomy'] == $current_term_object->taxonomy )
       
   471 				$r['current_category'] = get_queried_object_id();
       
   472 		}
   480 
   473 
   481 		if ( $hierarchical )
   474 		if ( $hierarchical )
   482 			$depth = $r['depth'];
   475 			$depth = $r['depth'];
   483 		else
   476 		else
   484 			$depth = -1; // Flat.
   477 			$depth = -1; // Flat.
   487 	}
   480 	}
   488 
   481 
   489 	if ( $title_li && 'list' == $style )
   482 	if ( $title_li && 'list' == $style )
   490 		$output .= '</ul></li>';
   483 		$output .= '</ul></li>';
   491 
   484 
   492 	$output = apply_filters( 'wp_list_categories', $output );
   485 	$output = apply_filters( 'wp_list_categories', $output, $args );
   493 
   486 
   494 	if ( $echo )
   487 	if ( $echo )
   495 		echo $output;
   488 		echo $output;
   496 	else
   489 	else
   497 		return $output;
   490 		return $output;
   533 	);
   526 	);
   534 	$args = wp_parse_args( $args, $defaults );
   527 	$args = wp_parse_args( $args, $defaults );
   535 
   528 
   536 	$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
   529 	$tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
   537 
   530 
   538 	if ( empty( $tags ) )
   531 	if ( empty( $tags ) || is_wp_error( $tags ) )
   539 		return;
   532 		return;
   540 
   533 
   541 	foreach ( $tags as $key => $tag ) {
   534 	foreach ( $tags as $key => $tag ) {
   542 		if ( 'edit' == $args['link'] )
   535 		if ( 'edit' == $args['link'] )
   543 			$link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
   536 			$link = get_edit_tag_link( $tag->term_id, $tag->taxonomy );
   544 		else
   537 		else
   545 			$link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
   538 			$link = get_term_link( intval($tag->term_id), $tag->taxonomy );
   546 		if ( is_wp_error( $link ) )
   539 		if ( is_wp_error( $link ) )
   547 			return false;
   540 			return false;
   548 
   541 
   549 		$tags[ $key ]->link = $link;
   542 		$tags[ $key ]->link = $link;
   550 		$tags[ $key ]->id = $tag->term_id;
   543 		$tags[ $key ]->id = $tag->term_id;
   577  * @return integer scaled count
   570  * @return integer scaled count
   578  */
   571  */
   579 function default_topic_count_scale( $count ) {
   572 function default_topic_count_scale( $count ) {
   580 	return round(log10($count + 1) * 100);
   573 	return round(log10($count + 1) * 100);
   581 }
   574 }
   582 
       
   583 
   575 
   584 /**
   576 /**
   585  * Generates a tag cloud (heatmap) from provided data.
   577  * Generates a tag cloud (heatmap) from provided data.
   586  *
   578  *
   587  * The text size is set by the 'smallest' and 'largest' arguments, which will
   579  * The text size is set by the 'smallest' and 'largest' arguments, which will
   611  * @param array $tags List of tags.
   603  * @param array $tags List of tags.
   612  * @param string|array $args Optional, override default arguments.
   604  * @param string|array $args Optional, override default arguments.
   613  * @return string
   605  * @return string
   614  */
   606  */
   615 function wp_generate_tag_cloud( $tags, $args = '' ) {
   607 function wp_generate_tag_cloud( $tags, $args = '' ) {
   616 	global $wp_rewrite;
       
   617 	$defaults = array(
   608 	$defaults = array(
   618 		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
   609 		'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
   619 		'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
   610 		'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
   620 		'topic_count_text_callback' => 'default_topic_count_text',
   611 		'topic_count_text_callback' => 'default_topic_count_text',
   621 		'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
   612 		'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
   642 		if ( 'RAND' == $order ) {
   633 		if ( 'RAND' == $order ) {
   643 			shuffle($tags);
   634 			shuffle($tags);
   644 		} else {
   635 		} else {
   645 			// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
   636 			// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
   646 			if ( 'name' == $orderby )
   637 			if ( 'name' == $orderby )
   647 				uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
   638 				uasort( $tags, '_wp_object_name_sort_cb' );
   648 			else
   639 			else
   649 				uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
   640 				uasort( $tags, '_wp_object_count_sort_cb' );
   650 
   641 
   651 			if ( 'DESC' == $order )
   642 			if ( 'DESC' == $order )
   652 				$tags = array_reverse( $tags, true );
   643 				$tags = array_reverse( $tags, true );
   653 		}
   644 		}
   654 	}
   645 	}
   678 		$count = $counts[ $key ];
   669 		$count = $counts[ $key ];
   679 		$real_count = $real_counts[ $key ];
   670 		$real_count = $real_counts[ $key ];
   680 		$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
   671 		$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
   681 		$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
   672 		$tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
   682 		$tag_name = $tags[ $key ]->name;
   673 		$tag_name = $tags[ $key ]->name;
   683 		$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $real_count ) ) . "' style='font-size: " .
   674 		$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: " .
   684 			( $smallest + ( ( $count - $min_count ) * $font_step ) )
   675 			str_replace( ',', '.', ( $smallest + ( ( $count - $min_count ) * $font_step ) ) )
   685 			. "$unit;'>$tag_name</a>";
   676 			. "$unit;'>$tag_name</a>";
   686 	}
   677 	}
   687 
   678 
   688 	switch ( $format ) :
   679 	switch ( $format ) :
   689 	case 'array' :
   680 	case 'array' :
   697 	default :
   688 	default :
   698 		$return = join( $separator, $a );
   689 		$return = join( $separator, $a );
   699 		break;
   690 		break;
   700 	endswitch;
   691 	endswitch;
   701 
   692 
   702     if ( $filter )
   693 	if ( $filter )
   703 		return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
   694 		return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
   704     else
   695 	else
   705 		return $return;
   696 		return $return;
       
   697 }
       
   698 
       
   699 /**
       
   700  * Callback for comparing objects based on name
       
   701  *
       
   702  * @since 3.1.0
       
   703  * @access private
       
   704  */
       
   705 function _wp_object_name_sort_cb( $a, $b ) {
       
   706 	return strnatcasecmp( $a->name, $b->name );
       
   707 }
       
   708 
       
   709 /**
       
   710  * Callback for comparing objects based on count
       
   711  *
       
   712  * @since 3.1.0
       
   713  * @access private
       
   714  */
       
   715 function _wp_object_count_sort_cb( $a, $b ) {
       
   716 	return ( $a->count > $b->count );
   706 }
   717 }
   707 
   718 
   708 //
   719 //
   709 // Helper functions
   720 // Helper functions
   710 //
   721 //
   743 		$walker = $args[2]['walker'];
   754 		$walker = $args[2]['walker'];
   744 
   755 
   745 	return call_user_func_array(array( &$walker, 'walk' ), $args );
   756 	return call_user_func_array(array( &$walker, 'walk' ), $args );
   746 }
   757 }
   747 
   758 
       
   759 /**
       
   760  * Create HTML list of categories.
       
   761  *
       
   762  * @package WordPress
       
   763  * @since 2.1.0
       
   764  * @uses Walker
       
   765  */
       
   766 class Walker_Category extends Walker {
       
   767 	/**
       
   768 	 * @see Walker::$tree_type
       
   769 	 * @since 2.1.0
       
   770 	 * @var string
       
   771 	 */
       
   772 	var $tree_type = 'category';
       
   773 
       
   774 	/**
       
   775 	 * @see Walker::$db_fields
       
   776 	 * @since 2.1.0
       
   777 	 * @todo Decouple this
       
   778 	 * @var array
       
   779 	 */
       
   780 	var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
       
   781 
       
   782 	/**
       
   783 	 * @see Walker::start_lvl()
       
   784 	 * @since 2.1.0
       
   785 	 *
       
   786 	 * @param string $output Passed by reference. Used to append additional content.
       
   787 	 * @param int $depth Depth of category. Used for tab indentation.
       
   788 	 * @param array $args Will only append content if style argument value is 'list'.
       
   789 	 */
       
   790 	function start_lvl( &$output, $depth = 0, $args = array() ) {
       
   791 		if ( 'list' != $args['style'] )
       
   792 			return;
       
   793 
       
   794 		$indent = str_repeat("\t", $depth);
       
   795 		$output .= "$indent<ul class='children'>\n";
       
   796 	}
       
   797 
       
   798 	/**
       
   799 	 * @see Walker::end_lvl()
       
   800 	 * @since 2.1.0
       
   801 	 *
       
   802 	 * @param string $output Passed by reference. Used to append additional content.
       
   803 	 * @param int $depth Depth of category. Used for tab indentation.
       
   804 	 * @param array $args Will only append content if style argument value is 'list'.
       
   805 	 */
       
   806 	function end_lvl( &$output, $depth = 0, $args = array() ) {
       
   807 		if ( 'list' != $args['style'] )
       
   808 			return;
       
   809 
       
   810 		$indent = str_repeat("\t", $depth);
       
   811 		$output .= "$indent</ul>\n";
       
   812 	}
       
   813 
       
   814 	/**
       
   815 	 * @see Walker::start_el()
       
   816 	 * @since 2.1.0
       
   817 	 *
       
   818 	 * @param string $output Passed by reference. Used to append additional content.
       
   819 	 * @param object $category Category data object.
       
   820 	 * @param int $depth Depth of category in reference to parents.
       
   821 	 * @param array $args
       
   822 	 */
       
   823 	function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
       
   824 		extract($args);
       
   825 
       
   826 		$cat_name = esc_attr( $category->name );
       
   827 		$cat_name = apply_filters( 'list_cats', $cat_name, $category );
       
   828 		$link = '<a href="' . esc_url( get_term_link($category) ) . '" ';
       
   829 		if ( $use_desc_for_title == 0 || empty($category->description) )
       
   830 			$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
       
   831 		else
       
   832 			$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
       
   833 		$link .= '>';
       
   834 		$link .= $cat_name . '</a>';
       
   835 
       
   836 		if ( !empty($feed_image) || !empty($feed) ) {
       
   837 			$link .= ' ';
       
   838 
       
   839 			if ( empty($feed_image) )
       
   840 				$link .= '(';
       
   841 
       
   842 			$link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $feed_type ) ) . '"';
       
   843 
       
   844 			if ( empty($feed) ) {
       
   845 				$alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
       
   846 			} else {
       
   847 				$title = ' title="' . $feed . '"';
       
   848 				$alt = ' alt="' . $feed . '"';
       
   849 				$name = $feed;
       
   850 				$link .= $title;
       
   851 			}
       
   852 
       
   853 			$link .= '>';
       
   854 
       
   855 			if ( empty($feed_image) )
       
   856 				$link .= $name;
       
   857 			else
       
   858 				$link .= "<img src='$feed_image'$alt$title" . ' />';
       
   859 
       
   860 			$link .= '</a>';
       
   861 
       
   862 			if ( empty($feed_image) )
       
   863 				$link .= ')';
       
   864 		}
       
   865 
       
   866 		if ( !empty($show_count) )
       
   867 			$link .= ' (' . intval($category->count) . ')';
       
   868 
       
   869 		if ( 'list' == $args['style'] ) {
       
   870 			$output .= "\t<li";
       
   871 			$class = 'cat-item cat-item-' . $category->term_id;
       
   872 			if ( !empty($current_category) ) {
       
   873 				$_current_category = get_term( $current_category, $category->taxonomy );
       
   874 				if ( $category->term_id == $current_category )
       
   875 					$class .=  ' current-cat';
       
   876 				elseif ( $category->term_id == $_current_category->parent )
       
   877 					$class .=  ' current-cat-parent';
       
   878 			}
       
   879 			$output .=  ' class="' . $class . '"';
       
   880 			$output .= ">$link\n";
       
   881 		} else {
       
   882 			$output .= "\t$link<br />\n";
       
   883 		}
       
   884 	}
       
   885 
       
   886 	/**
       
   887 	 * @see Walker::end_el()
       
   888 	 * @since 2.1.0
       
   889 	 *
       
   890 	 * @param string $output Passed by reference. Used to append additional content.
       
   891 	 * @param object $page Not used.
       
   892 	 * @param int $depth Depth of category. Not used.
       
   893 	 * @param array $args Only uses 'list' for whether should append to output.
       
   894 	 */
       
   895 	function end_el( &$output, $page, $depth = 0, $args = array() ) {
       
   896 		if ( 'list' != $args['style'] )
       
   897 			return;
       
   898 
       
   899 		$output .= "</li>\n";
       
   900 	}
       
   901 
       
   902 }
       
   903 
       
   904 /**
       
   905  * Create HTML dropdown list of Categories.
       
   906  *
       
   907  * @package WordPress
       
   908  * @since 2.1.0
       
   909  * @uses Walker
       
   910  */
       
   911 class Walker_CategoryDropdown extends Walker {
       
   912 	/**
       
   913 	 * @see Walker::$tree_type
       
   914 	 * @since 2.1.0
       
   915 	 * @var string
       
   916 	 */
       
   917 	var $tree_type = 'category';
       
   918 
       
   919 	/**
       
   920 	 * @see Walker::$db_fields
       
   921 	 * @since 2.1.0
       
   922 	 * @todo Decouple this
       
   923 	 * @var array
       
   924 	 */
       
   925 	var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
       
   926 
       
   927 	/**
       
   928 	 * @see Walker::start_el()
       
   929 	 * @since 2.1.0
       
   930 	 *
       
   931 	 * @param string $output Passed by reference. Used to append additional content.
       
   932 	 * @param object $category Category data object.
       
   933 	 * @param int $depth Depth of category. Used for padding.
       
   934 	 * @param array $args Uses 'selected' and 'show_count' keys, if they exist.
       
   935 	 */
       
   936 	function start_el( &$output, $category, $depth, $args, $id = 0 ) {
       
   937 		$pad = str_repeat('&nbsp;', $depth * 3);
       
   938 
       
   939 		$cat_name = apply_filters('list_cats', $category->name, $category);
       
   940 		$output .= "\t<option class=\"level-$depth\" value=\"".$category->term_id."\"";
       
   941 		if ( $category->term_id == $args['selected'] )
       
   942 			$output .= ' selected="selected"';
       
   943 		$output .= '>';
       
   944 		$output .= $pad.$cat_name;
       
   945 		if ( $args['show_count'] )
       
   946 			$output .= '&nbsp;&nbsp;('. $category->count .')';
       
   947 		$output .= "</option>\n";
       
   948 	}
       
   949 }
       
   950 
   748 //
   951 //
   749 // Tags
   952 // Tags
   750 //
   953 //
   751 
   954 
   752 /**
   955 /**
   753  * Retrieve the link to the tag.
   956  * Retrieve the link to the tag.
   754  *
   957  *
   755  * @since 2.3.0
   958  * @since 2.3.0
   756  * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
   959  * @see get_term_link()
   757  *
   960  *
   758  * @param int $tag_id Tag (term) ID.
   961  * @param int|object $tag Tag ID or object.
   759  * @return string
   962  * @return string Link on success, empty string if tag does not exist.
   760  */
   963  */
   761 function get_tag_link( $tag_id ) {
   964 function get_tag_link( $tag ) {
   762 	global $wp_rewrite;
   965 	if ( ! is_object( $tag ) )
   763 	$taglink = $wp_rewrite->get_tag_permastruct();
   966 		$tag = (int) $tag;
   764 
   967 
   765 	$tag = &get_term( $tag_id, 'post_tag' );
   968 	$tag = get_term_link( $tag, 'post_tag' );
       
   969 
   766 	if ( is_wp_error( $tag ) )
   970 	if ( is_wp_error( $tag ) )
   767 		return $tag;
   971 		return '';
   768 	$slug = $tag->slug;
   972 
   769 
   973 	return $tag;
   770 	if ( empty( $taglink ) ) {
       
   771 		$file = get_option( 'home' ) . '/';
       
   772 		$taglink = $file . '?tag=' . $slug;
       
   773 	} else {
       
   774 		$taglink = str_replace( '%tag%', $slug, $taglink );
       
   775 		$taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' );
       
   776 	}
       
   777 	return apply_filters( 'tag_link', $taglink, $tag_id );
       
   778 }
   974 }
   779 
   975 
   780 /**
   976 /**
   781  * Retrieve the tags for a post.
   977  * Retrieve the tags for a post.
   782  *
   978  *
   797  * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
   993  * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
   798  *
   994  *
   799  * @param string $before Optional. Before tags.
   995  * @param string $before Optional. Before tags.
   800  * @param string $sep Optional. Between tags.
   996  * @param string $sep Optional. Between tags.
   801  * @param string $after Optional. After tags.
   997  * @param string $after Optional. After tags.
       
   998  * @param int $id Optional. Post ID. Defaults to the current post.
   802  * @return string
   999  * @return string
   803  */
  1000  */
   804 function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
  1001 function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
   805 	return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
  1002 	return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
   806 }
  1003 }
   807 
  1004 
   808 /**
  1005 /**
   809  * Retrieve the tags for a post.
  1006  * Retrieve the tags for a post.
   810  *
  1007  *
   837  * Retrieve term description.
  1034  * Retrieve term description.
   838  *
  1035  *
   839  * @since 2.8
  1036  * @since 2.8
   840  *
  1037  *
   841  * @param int $term Optional. Term ID. Will use global term ID by default.
  1038  * @param int $term Optional. Term ID. Will use global term ID by default.
       
  1039  * @param string $taxonomy Optional taxonomy name. Defaults to 'post_tag'.
   842  * @return string Term description, available.
  1040  * @return string Term description, available.
   843  */
  1041  */
   844 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
  1042 function term_description( $term = 0, $taxonomy = 'post_tag' ) {
   845 	if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
  1043 	if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
   846 		global $wp_query;
  1044 		$term = get_queried_object();
   847 		$term = $wp_query->get_queried_object();
       
   848 		$taxonomy = $term->taxonomy;
  1045 		$taxonomy = $term->taxonomy;
   849 		$term = $term->term_id;
  1046 		$term = $term->term_id;
   850 	}
  1047 	}
   851 	return get_term_field( 'description', $term, $taxonomy );
  1048 	$description = get_term_field( 'description', $term, $taxonomy );
       
  1049 	return is_wp_error( $description ) ? '' : $description;
   852 }
  1050 }
   853 
  1051 
   854 /**
  1052 /**
   855  * Retrieve the terms of the taxonomy that are attached to the post.
  1053  * Retrieve the terms of the taxonomy that are attached to the post.
   856  *
  1054  *
   857  * This function can only be used within the loop.
       
   858  *
       
   859  * @since 2.5.0
  1055  * @since 2.5.0
   860  *
  1056  *
   861  * @param int $id Post ID. Is not optional.
  1057  * @param int $id Post ID.
   862  * @param string $taxonomy Taxonomy name.
  1058  * @param string $taxonomy Taxonomy name.
   863  * @return array|bool False on failure. Array of term objects on success.
  1059  * @return array|bool False on failure. Array of term objects on success.
   864  */
  1060  */
   865 function get_the_terms( $id = 0, $taxonomy ) {
  1061 function get_the_terms( $id, $taxonomy ) {
   866 	global $post;
  1062 	global $post;
   867 
  1063 
   868  	$id = (int) $id;
  1064  	$id = (int) $id;
   869 
  1065 
   870 	if ( !$id ) {
  1066 	if ( !$id ) {
   871 		if ( !$post->ID )
  1067 		if ( empty( $post->ID ) )
   872 			return false;
  1068 			return false;
   873 		else
  1069 		else
   874 			$id = (int) $post->ID;
  1070 			$id = (int) $post->ID;
   875 	}
  1071 	}
   876 
  1072 
   877 	$terms = get_object_term_cache( $id, $taxonomy );
  1073 	$terms = get_object_term_cache( $id, $taxonomy );
   878 	if ( false === $terms )
  1074 	if ( false === $terms ) {
   879 		$terms = wp_get_object_terms( $id, $taxonomy );
  1075 		$terms = wp_get_object_terms( $id, $taxonomy );
       
  1076 		wp_cache_add($id, $terms, $taxonomy . '_relationships');
       
  1077 	}
       
  1078 
       
  1079 	$terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy );
   880 
  1080 
   881 	if ( empty( $terms ) )
  1081 	if ( empty( $terms ) )
   882 		return false;
  1082 		return false;
   883 
  1083 
   884 	return $terms;
  1084 	return $terms;
   894  * @param string $before Optional. Before list.
  1094  * @param string $before Optional. Before list.
   895  * @param string $sep Optional. Separate items using this.
  1095  * @param string $sep Optional. Separate items using this.
   896  * @param string $after Optional. After list.
  1096  * @param string $after Optional. After list.
   897  * @return string
  1097  * @return string
   898  */
  1098  */
   899 function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
  1099 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
   900 	$terms = get_the_terms( $id, $taxonomy );
  1100 	$terms = get_the_terms( $id, $taxonomy );
   901 
  1101 
   902 	if ( is_wp_error( $terms ) )
  1102 	if ( is_wp_error( $terms ) )
   903 		return $terms;
  1103 		return $terms;
   904 
  1104 
   907 
  1107 
   908 	foreach ( $terms as $term ) {
  1108 	foreach ( $terms as $term ) {
   909 		$link = get_term_link( $term, $taxonomy );
  1109 		$link = get_term_link( $term, $taxonomy );
   910 		if ( is_wp_error( $link ) )
  1110 		if ( is_wp_error( $link ) )
   911 			return $link;
  1111 			return $link;
   912 		$term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
  1112 		$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
   913 	}
  1113 	}
   914 
  1114 
   915 	$term_links = apply_filters( "term_links-$taxonomy", $term_links );
  1115 	$term_links = apply_filters( "term_links-$taxonomy", $term_links );
   916 
  1116 
   917 	return $before . join( $sep, $term_links ) . $after;
  1117 	return $before . join( $sep, $term_links ) . $after;
   920 /**
  1120 /**
   921  * Display the terms in a list.
  1121  * Display the terms in a list.
   922  *
  1122  *
   923  * @since 2.5.0
  1123  * @since 2.5.0
   924  *
  1124  *
   925  * @param int $id Term ID.
  1125  * @param int $id Post ID.
   926  * @param string $taxonomy Taxonomy name.
  1126  * @param string $taxonomy Taxonomy name.
   927  * @param string $before Optional. Before list.
  1127  * @param string $before Optional. Before list.
   928  * @param string $sep Optional. Separate items using this.
  1128  * @param string $sep Optional. Separate items using this.
   929  * @param string $after Optional. After list.
  1129  * @param string $after Optional. After list.
   930  * @return null|bool False on WordPress error. Returns null when displaying.
  1130  * @return null|bool False on WordPress error. Returns null when displaying.
   937 
  1137 
   938 	echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
  1138 	echo apply_filters('the_terms', $term_list, $taxonomy, $before, $sep, $after);
   939 }
  1139 }
   940 
  1140 
   941 /**
  1141 /**
       
  1142  * Check if the current post has any of given category.
       
  1143  *
       
  1144  * @since 3.1.0
       
  1145  *
       
  1146  * @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
       
  1147  * @param int|object $post Optional. Post to check instead of the current post.
       
  1148  * @return bool True if the current post has any of the given categories (or any category, if no category specified).
       
  1149  */
       
  1150 function has_category( $category = '', $post = null ) {
       
  1151 	return has_term( $category, 'category', $post );
       
  1152 }
       
  1153 
       
  1154 /**
   942  * Check if the current post has any of given tags.
  1155  * Check if the current post has any of given tags.
   943  *
  1156  *
   944  * The given tags are checked against the post's tags' term_ids, names and slugs.
  1157  * The given tags are checked against the post's tags' term_ids, names and slugs.
   945  * Tags given as integers will only be checked against the post's tags' term_ids.
  1158  * Tags given as integers will only be checked against the post's tags' term_ids.
   946  * If no tags are given, determines if post has any tags.
  1159  * If no tags are given, determines if post has any tags.
   949  * Prior to v2.7, this function could only be used in the WordPress Loop.
  1162  * Prior to v2.7, this function could only be used in the WordPress Loop.
   950  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
  1163  * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
   951  *
  1164  *
   952  * @since 2.6.0
  1165  * @since 2.6.0
   953  *
  1166  *
   954  * @uses is_object_in_term()
       
   955  *
       
   956  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
  1167  * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
   957  * @param int|post object Optional.  Post to check instead of the current post. @since 2.7.0
  1168  * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
   958  * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
  1169  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
   959  */
  1170  */
   960 function has_tag( $tag = '', $_post = null ) {
  1171 function has_tag( $tag = '', $post = null ) {
   961 	if ( $_post ) {
  1172 	return has_term( $tag, 'post_tag', $post );
   962 		$_post = get_post( $_post );
  1173 }
   963 	} else {
  1174 
   964 		$_post =& $GLOBALS['post'];
  1175 /**
   965 	}
  1176  * Check if the current post has any of given terms.
   966 
  1177  *
   967 	if ( !$_post )
  1178  * The given terms are checked against the post's terms' term_ids, names and slugs.
       
  1179  * Terms given as integers will only be checked against the post's terms' term_ids.
       
  1180  * If no terms are given, determines if post has any terms.
       
  1181  *
       
  1182  * @since 3.1.0
       
  1183  *
       
  1184  * @param string|int|array $term Optional. The term name/term_id/slug or array of them to check for.
       
  1185  * @param string $taxonomy Taxonomy name
       
  1186  * @param int|object $post Optional. Post to check instead of the current post.
       
  1187  * @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
       
  1188  */
       
  1189 function has_term( $term = '', $taxonomy = '', $post = null ) {
       
  1190 	$post = get_post($post);
       
  1191 
       
  1192 	if ( !$post )
   968 		return false;
  1193 		return false;
   969 
  1194 
   970 	$r = is_object_in_term( $_post->ID, 'post_tag', $tag );
  1195 	$r = is_object_in_term( $post->ID, $taxonomy, $term );
   971 	if ( is_wp_error( $r ) )
  1196 	if ( is_wp_error( $r ) )
   972 		return false;
  1197 		return false;
       
  1198 
   973 	return $r;
  1199 	return $r;
   974 }
  1200 }
   975 
       
   976 ?>