source: trunk/www.guidonia.net/wp/wp-includes/category-template.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 14 years ago
File size: 29.4 KB
Line 
1<?php
2/**
3 * Category Template Tags and API.
4 *
5 * @package WordPress
6 * @subpackage Template
7 */
8
9/**
10 * Retrieve category link URL.
11 *
12 * @since 1.0.0
13 * @uses apply_filters() Calls 'category_link' filter on category link and category ID.
14 *
15 * @param int $category_id Category ID.
16 * @return string
17 */
18function get_category_link( $category_id ) {
19 global $wp_rewrite;
20 $catlink = $wp_rewrite->get_category_permastruct();
21
22 if ( empty( $catlink ) ) {
23 $file = get_option( 'home' ) . '/';
24 $catlink = $file . '?cat=' . $category_id;
25 } else {
26 $category = &get_category( $category_id );
27 if ( is_wp_error( $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}
41
42/**
43 * Retrieve category parents with separator.
44 *
45 * @since 1.2.0
46 *
47 * @param int $id Category ID.
48 * @param bool $link Optional, default is false. Whether to format with link.
49 * @param string $separator Optional, default is '/'. How to separate categories.
50 * @param bool $nicename Optional, default is false. Whether to use nice name for display.
51 * @param array $visited Optional. Already linked to categories to prevent duplicates.
52 * @return string
53 */
54function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
55 $chain = '';
56 $parent = &get_category( $id );
57 if ( is_wp_error( $parent ) )
58 return $parent;
59
60 if ( $nicename )
61 $name = $parent->slug;
62 else
63 $name = $parent->cat_name;
64
65 if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
66 $visited[] = $parent->parent;
67 $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
68 }
69
70 if ( $link )
71 $chain .= '<a href="' . get_category_link( $parent->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $parent->cat_name ) . '">'.$name.'</a>' . $separator;
72 else
73 $chain .= $name.$separator;
74 return $chain;
75}
76
77/**
78 * Retrieve post categories.
79 *
80 * @since 0.71
81 * @uses $post
82 *
83 * @param int $id Optional, default to current post ID. The post ID.
84 * @return array
85 */
86function get_the_category( $id = false ) {
87 global $post;
88
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();
103
104 foreach ( (array) array_keys( $categories ) as $key ) {
105 _make_cat_compat( $categories[$key] );
106 }
107
108 return $categories;
109}
110
111/**
112 * Sort categories by name.
113 *
114 * Used by usort() as a callback, should not be used directly. Can actually be
115 * used to sort any term object.
116 *
117 * @since 2.3.0
118 * @access private
119 *
120 * @param object $a
121 * @param object $b
122 * @return int
123 */
124function _usort_terms_by_name( $a, $b ) {
125 return strcmp( $a->name, $b->name );
126}
127
128/**
129 * Sort categories by ID.
130 *
131 * Used by usort() as a callback, should not be used directly. Can actually be
132 * used to sort any term object.
133 *
134 * @since 2.3.0
135 * @access private
136 *
137 * @param object $a
138 * @param object $b
139 * @return int
140 */
141function _usort_terms_by_ID( $a, $b ) {
142 if ( $a->term_id > $b->term_id )
143 return 1;
144 elseif ( $a->term_id < $b->term_id )
145 return -1;
146 else
147 return 0;
148}
149
150/**
151 * Retrieve category name based on category ID.
152 *
153 * @since 0.71
154 *
155 * @param int $cat_ID Category ID.
156 * @return string Category name.
157 */
158function get_the_category_by_ID( $cat_ID ) {
159 $cat_ID = (int) $cat_ID;
160 $category = &get_category( $cat_ID );
161 if ( is_wp_error( $category ) )
162 return $category;
163 return $category->name;
164}
165
166/**
167 * Retrieve category list in either HTML list or custom format.
168 *
169 * @since 1.5.1
170 *
171 * @param string $separator Optional, default is empty string. Separator for between the categories.
172 * @param string $parents Optional. How to display the parents.
173 * @param int $post_id Optional. Post ID to retrieve categories.
174 * @return string
175 */
176function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
177 global $wp_rewrite;
178 $categories = get_the_category( $post_id );
179 if ( empty( $categories ) )
180 return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
181
182 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
183
184 $thelist = '';
185 if ( '' == $separator ) {
186 $thelist .= '<ul class="post-categories">';
187 foreach ( $categories as $category ) {
188 $thelist .= "\n\t<li>";
189 switch ( strtolower( $parents ) ) {
190 case 'multiple':
191 if ( $category->parent )
192 $thelist .= get_category_parents( $category->parent, true, $separator );
193 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a></li>';
194 break;
195 case 'single':
196 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
197 if ( $category->parent )
198 $thelist .= get_category_parents( $category->parent, false, $separator );
199 $thelist .= $category->name.'</a></li>';
200 break;
201 case '':
202 default:
203 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
204 }
205 }
206 $thelist .= '</ul>';
207 } else {
208 $i = 0;
209 foreach ( $categories as $category ) {
210 if ( 0 < $i )
211 $thelist .= $separator . ' ';
212 switch ( strtolower( $parents ) ) {
213 case 'multiple':
214 if ( $category->parent )
215 $thelist .= get_category_parents( $category->parent, true, $separator );
216 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
217 break;
218 case 'single':
219 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
220 if ( $category->parent )
221 $thelist .= get_category_parents( $category->parent, false, $separator );
222 $thelist .= "$category->cat_name</a>";
223 break;
224 case '':
225 default:
226 $thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a>';
227 }
228 ++$i;
229 }
230 }
231 return apply_filters( 'the_category', $thelist, $separator, $parents );
232}
233
234
235/**
236 * Check if the current post in within any of the given categories.
237 *
238 * The given categories are checked against the post's categories' term_ids, names and slugs.
239 * Categories given as integers will only be checked against the post's categories' term_ids.
240 *
241 * Prior to v2.5 of WordPress, category names were not supported.
242 * Prior to v2.7, category slugs were not supported.
243 * Prior to v2.7, only one category could be compared: in_category( $single_category ).
244 * 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.
246 *
247 * @since 1.2.0
248 *
249 * @uses is_object_in_term()
250 *
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.
254 */
255function in_category( $category, $_post = null ) {
256 if ( empty( $category ) )
257 return false;
258
259 if ( $_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}
273
274/**
275 * Display the category list for the post.
276 *
277 * @since 0.71
278 *
279 * @param string $separator Optional, default is empty string. Separator for between the categories.
280 * @param string $parents Optional. How to display the parents.
281 * @param int $post_id Optional. Post ID to retrieve categories.
282 */
283function the_category( $separator = '', $parents='', $post_id = false ) {
284 echo get_the_category_list( $separator, $parents, $post_id );
285}
286
287/**
288 * Retrieve category description.
289 *
290 * @since 1.0.0
291 *
292 * @param int $category Optional. Category ID. Will use global category ID by default.
293 * @return string Category description, available.
294 */
295function category_description( $category = 0 ) {
296 return term_description( $category, 'category' );
297}
298
299/**
300 * Display or retrieve the HTML dropdown list of categories.
301 *
302 * The list of arguments is below:
303 * 'show_option_all' (string) - Text to display for showing all categories.
304 * 'show_option_none' (string) - Text to display for showing no categories.
305 * 'orderby' (string) default is 'ID' - What column to use for ordering the
306 * categories.
307 * '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
310 * in the category.
311 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
312 * don't have any posts attached to them.
313 * 'child_of' (int) default is 0 - See {@link get_categories()}.
314 * 'exclude' (string) - See {@link get_categories()}.
315 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
316 * 'depth' (int) - The max depth.
317 * 'tab_index' (int) - Tab index for select element.
318 * 'name' (string) - The name attribute value for selected element.
319 * 'class' (string) - The class attribute value for selected element.
320 * 'selected' (int) - Which category ID is selected.
321 *
322 * 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
324 * display all of the categories. When it is enabled it will use the value in
325 * the 'depth' argument.
326 *
327 * @since 2.1.0
328 *
329 * @param string|array $args Optional. Override default arguments.
330 * @return string HTML content only if 'echo' argument is 0.
331 */
332function wp_dropdown_categories( $args = '' ) {
333 $defaults = array(
334 'show_option_all' => '', 'show_option_none' => '',
335 'orderby' => 'id', 'order' => 'ASC',
336 'show_last_update' => 0, 'show_count' => 0,
337 'hide_empty' => 1, 'child_of' => 0,
338 'exclude' => '', 'echo' => 1,
339 'selected' => 0, 'hierarchical' => 0,
340 'name' => 'cat', 'class' => 'postform',
341 'depth' => 0, 'tab_index' => 0
342 );
343
344 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
345
346 $r = wp_parse_args( $args, $defaults );
347 $r['include_last_update_time'] = $r['show_last_update'];
348 extract( $r );
349
350 $tab_index_attribute = '';
351 if ( (int) $tab_index > 0 )
352 $tab_index_attribute = " tabindex=\"$tab_index\"";
353
354 $categories = get_categories( $r );
355
356 $output = '';
357 if ( ! empty( $categories ) ) {
358 $output = "<select name='$name' id='$name' class='$class' $tab_index_attribute>\n";
359
360 if ( $show_option_all ) {
361 $show_option_all = apply_filters( 'list_cats', $show_option_all );
362 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
363 $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
364 }
365
366 if ( $show_option_none ) {
367 $show_option_none = apply_filters( 'list_cats', $show_option_none );
368 $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
369 $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
370 }
371
372 if ( $hierarchical )
373 $depth = $r['depth']; // Walk the full depth.
374 else
375 $depth = -1; // Flat.
376
377 $output .= walk_category_dropdown_tree( $categories, $depth, $r );
378 $output .= "</select>\n";
379 }
380
381 $output = apply_filters( 'wp_dropdown_cats', $output );
382
383 if ( $echo )
384 echo $output;
385
386 return $output;
387}
388
389/**
390 * Display or retrieve the HTML list of categories.
391 *
392 * The list of arguments is below:
393 * 'show_option_all' (string) - Text to display for showing all categories.
394 * 'orderby' (string) default is 'ID' - What column to use for ordering the
395 * categories.
396 * 'order' (string) default is 'ASC' - What direction to order categories.
397 * 'show_last_update' (bool|int) default is 0 - See {@link
398 * walk_category_dropdown_tree()}
399 * 'show_count' (bool|int) default is 0 - Whether to show how many posts are
400 * in the category.
401 * 'hide_empty' (bool|int) default is 1 - Whether to hide categories that
402 * don't have any posts attached to them.
403 * 'use_desc_for_title' (bool|int) default is 1 - Whether to use the
404 * description instead of the category title.
405 * 'feed' - See {@link get_categories()}.
406 * 'feed_type' - See {@link get_categories()}.
407 * 'feed_image' - See {@link get_categories()}.
408 * 'child_of' (int) default is 0 - See {@link get_categories()}.
409 * 'exclude' (string) - See {@link get_categories()}.
410 * 'exclude_tree' (string) - See {@link get_categories()}.
411 * 'echo' (bool|int) default is 1 - Whether to display or retrieve content.
412 * 'current_category' (int) - See {@link get_categories()}.
413 * 'hierarchical' (bool) - See {@link get_categories()}.
414 * 'title_li' (string) - See {@link get_categories()}.
415 * 'depth' (int) - The max depth.
416 *
417 * @since 2.1.0
418 *
419 * @param string|array $args Optional. Override default arguments.
420 * @return string HTML content only if 'echo' argument is 0.
421 */
422function wp_list_categories( $args = '' ) {
423 $defaults = array(
424 'show_option_all' => '', 'orderby' => 'name',
425 'order' => 'ASC', 'show_last_update' => 0,
426 'style' => 'list', 'show_count' => 0,
427 'hide_empty' => 1, 'use_desc_for_title' => 1,
428 'child_of' => 0, 'feed' => '', 'feed_type' => '',
429 'feed_image' => '', 'exclude' => '', 'exclude_tree' => '', 'current_category' => 0,
430 'hierarchical' => true, 'title_li' => __( 'Categories' ),
431 'echo' => 1, 'depth' => 0
432 );
433
434 $r = wp_parse_args( $args, $defaults );
435
436 if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
437 $r['pad_counts'] = true;
438 }
439
440 if ( isset( $r['show_date'] ) ) {
441 $r['include_last_update_time'] = $r['show_date'];
442 }
443
444 if ( true == $r['hierarchical'] ) {
445 $r['exclude_tree'] = $r['exclude'];
446 $r['exclude'] = '';
447 }
448
449 extract( $r );
450
451 $categories = get_categories( $r );
452
453 $output = '';
454 if ( $title_li && 'list' == $style )
455 $output = '<li class="categories">' . $r['title_li'] . '<ul>';
456
457 if ( empty( $categories ) ) {
458 if ( 'list' == $style )
459 $output .= '<li>' . __( "No categories" ) . '</li>';
460 else
461 $output .= __( "No categories" );
462 } else {
463 global $wp_query;
464
465 if( !empty( $show_option_all ) )
466 if ( 'list' == $style )
467 $output .= '<li><a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a></li>';
468 else
469 $output .= '<a href="' . get_bloginfo( 'url' ) . '">' . $show_option_all . '</a>';
470
471 if ( empty( $r['current_category'] ) && is_category() )
472 $r['current_category'] = $wp_query->get_queried_object_id();
473
474 if ( $hierarchical )
475 $depth = $r['depth'];
476 else
477 $depth = -1; // Flat.
478
479 $output .= walk_category_tree( $categories, $depth, $r );
480 }
481
482 if ( $title_li && 'list' == $style )
483 $output .= '</ul></li>';
484
485 $output = apply_filters( 'wp_list_categories', $output );
486
487 if ( $echo )
488 echo $output;
489 else
490 return $output;
491}
492
493/**
494 * Display tag cloud.
495 *
496 * The text size is set by the 'smallest' and 'largest' arguments, which will
497 * use the 'unit' argument value for the CSS text size unit. The 'format'
498 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
499 * 'format' argument will separate tags with spaces. The list value for the
500 * 'format' argument will format the tags in a UL HTML list. The array value for
501 * the 'format' argument will return in PHP array type format.
502 *
503 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
504 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
505 *
506 * The 'number' argument is how many tags to return. By default, the limit will
507 * be to return the top 45 tags in the tag cloud list.
508 *
509 * The 'topic_count_text_callback' argument is a function, which, given the count
510 * of the posts with that tag, returns a text for the tooltip of the tag link.
511 *
512 * The 'exclude' and 'include' arguments are used for the {@link get_tags()}
513 * function. Only one should be used, because only one will be used and the
514 * other ignored, if they are both set.
515 *
516 * @since 2.3.0
517 *
518 * @param array|string $args Optional. Override default arguments.
519 * @return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
520 */
521function wp_tag_cloud( $args = '' ) {
522 $defaults = array(
523 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
524 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
525 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
526 );
527 $args = wp_parse_args( $args, $defaults );
528
529 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags
530
531 if ( empty( $tags ) )
532 return;
533
534 foreach ( $tags as $key => $tag ) {
535 if ( 'edit' == $args['link'] )
536 $link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
537 else
538 $link = get_term_link( intval($tag->term_id), $args['taxonomy'] );
539 if ( is_wp_error( $link ) )
540 return false;
541
542 $tags[ $key ]->link = $link;
543 $tags[ $key ]->id = $tag->term_id;
544 }
545
546 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
547
548 $return = apply_filters( 'wp_tag_cloud', $return, $args );
549
550 if ( 'array' == $args['format'] || empty($args['echo']) )
551 return $return;
552
553 echo $return;
554}
555
556/**
557 * Default text for tooltip for tag links
558 *
559 * @param integer $count number of posts with that tag
560 * @return string text for the tooltip of a tag link.
561 */
562function default_topic_count_text( $count ) {
563 return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
564}
565
566/**
567 * Generates a tag cloud (heatmap) from provided data.
568 *
569 * The text size is set by the 'smallest' and 'largest' arguments, which will
570 * use the 'unit' argument value for the CSS text size unit. The 'format'
571 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the
572 * 'format' argument will separate tags with spaces. The list value for the
573 * 'format' argument will format the tags in a UL HTML list. The array value for
574 * the 'format' argument will return in PHP array type format.
575 *
576 * The 'tag_cloud_sort' filter allows you to override the sorting done
577 * by the 'orderby' argument; passed to the filter: $tags array and $args array.
578 *
579 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
580 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
581 * 'RAND'.
582 *
583 * The 'number' argument is how many tags to return. By default, the limit will
584 * be to return the entire tag cloud list.
585 *
586 * The 'topic_count_text_callback' argument is a function, which given the count
587 * of the posts with that tag returns a text for the tooltip of the tag link.
588 *
589 * @todo Complete functionality.
590 * @since 2.3.0
591 *
592 * @param array $tags List of tags.
593 * @param string|array $args Optional, override default arguments.
594 * @return string
595 */
596function wp_generate_tag_cloud( $tags, $args = '' ) {
597 global $wp_rewrite;
598 $defaults = array(
599 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
600 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
601 'topic_count_text_callback' => 'default_topic_count_text',
602 'filter' => 1,
603 );
604
605 if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
606 $body = 'return sprintf (
607 _n('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count),
608 number_format_i18n( $count ));';
609 $args['topic_count_text_callback'] = create_function('$count', $body);
610 }
611
612 $args = wp_parse_args( $args, $defaults );
613
614 extract( $args );
615
616 if ( empty( $tags ) )
617 return;
618
619 // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
620 if ( 'name' == $orderby )
621 uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
622 else
623 uasort( $tags, create_function('$a, $b', 'return ($a->count > $b->count);') );
624
625 $tags = apply_filters( 'tag_cloud_sort', $tags, $args );
626
627 if ( 'DESC' == $order )
628 $tags = array_reverse( $tags, true );
629 elseif ( 'RAND' == $order ) {
630 $keys = (array) array_rand( $tags, count( $tags ) );
631 $temp = array();
632 foreach ( $keys as $key )
633 $temp[$key] = $tags[$key];
634
635 $tags = $temp;
636 $temp = null;
637 unset( $temp );
638 }
639
640 if ( $number > 0 )
641 $tags = array_slice($tags, 0, $number);
642
643 $counts = array();
644 foreach ( (array) $tags as $key => $tag )
645 $counts[ $key ] = $tag->count;
646
647 $min_count = min( $counts );
648 $spread = max( $counts ) - $min_count;
649 if ( $spread <= 0 )
650 $spread = 1;
651 $font_spread = $largest - $smallest;
652 if ( $font_spread < 0 )
653 $font_spread = 1;
654 $font_step = $font_spread / $spread;
655
656 $a = array();
657
658 $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
659
660 foreach ( $tags as $key => $tag ) {
661 $count = $counts[ $key ];
662 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
663 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
664 $tag_name = $tags[ $key ]->name;
665 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " .
666 ( $smallest + ( ( $count - $min_count ) * $font_step ) )
667 . "$unit;'>$tag_name</a>";
668 }
669
670 switch ( $format ) :
671 case 'array' :
672 $return =& $a;
673 break;
674 case 'list' :
675 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
676 $return .= join( "</li>\n\t<li>", $a );
677 $return .= "</li>\n</ul>\n";
678 break;
679 default :
680 $return = join( "\n", $a );
681 break;
682 endswitch;
683
684 if ( $filter )
685 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
686 else
687 return $return;
688}
689
690//
691// Helper functions
692//
693
694/**
695 * Retrieve HTML list content for category list.
696 *
697 * @uses Walker_Category to create HTML list content.
698 * @since 2.1.0
699 * @see Walker_Category::walk() for parameters and return description.
700 */
701function walk_category_tree() {
702 $args = func_get_args();
703 // the user's options are the third parameter
704 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
705 $walker = new Walker_Category;
706 else
707 $walker = $args[2]['walker'];
708
709 return call_user_func_array(array( &$walker, 'walk' ), $args );
710}
711
712/**
713 * Retrieve HTML dropdown (select) content for category list.
714 *
715 * @uses Walker_CategoryDropdown to create HTML dropdown content.
716 * @since 2.1.0
717 * @see Walker_CategoryDropdown::walk() for parameters and return description.
718 */
719function walk_category_dropdown_tree() {
720 $args = func_get_args();
721 // the user's options are the third parameter
722 if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
723 $walker = new Walker_CategoryDropdown;
724 else
725 $walker = $args[2]['walker'];
726
727 return call_user_func_array(array( &$walker, 'walk' ), $args );
728}
729
730//
731// Tags
732//
733
734/**
735 * Retrieve the link to the tag.
736 *
737 * @since 2.3.0
738 * @uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
739 *
740 * @param int $tag_id Tag (term) ID.
741 * @return string
742 */
743function get_tag_link( $tag_id ) {
744 global $wp_rewrite;
745 $taglink = $wp_rewrite->get_tag_permastruct();
746
747 $tag = &get_term( $tag_id, 'post_tag' );
748 if ( is_wp_error( $tag ) )
749 return $tag;
750 $slug = $tag->slug;
751
752 if ( empty( $taglink ) ) {
753 $file = get_option( 'home' ) . '/';
754 $taglink = $file . '?tag=' . $slug;
755 } else {
756 $taglink = str_replace( '%tag%', $slug, $taglink );
757 $taglink = get_option( 'home' ) . user_trailingslashit( $taglink, 'category' );
758 }
759 return apply_filters( 'tag_link', $taglink, $tag_id );
760}
761
762/**
763 * Retrieve the tags for a post.
764 *
765 * @since 2.3.0
766 * @uses apply_filters() Calls 'get_the_tags' filter on the list of post tags.
767 *
768 * @param int $id Post ID.
769 * @return array
770 */
771function get_the_tags( $id = 0 ) {
772 return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
773}
774
775/**
776 * Retrieve the tags for a post formatted as a string.
777 *
778 * @since 2.3.0
779 * @uses apply_filters() Calls 'the_tags' filter on string list of tags.
780 *
781 * @param string $before Optional. Before tags.
782 * @param string $sep Optional. Between tags.
783 * @param string $after Optional. After tags.
784 * @return string
785 */
786function get_the_tag_list( $before = '', $sep = '', $after = '' ) {
787 return apply_filters( 'the_tags', get_the_term_list( 0, 'post_tag', $before, $sep, $after ), $before, $sep, $after);
788}
789
790/**
791 * Retrieve the tags for a post.
792 *
793 * @since 2.3.0
794 *
795 * @param string $before Optional. Before list.
796 * @param string $sep Optional. Separate items using this.
797 * @param string $after Optional. After list.
798 * @return string
799 */
800function the_tags( $before = null, $sep = ', ', $after = '' ) {
801 if ( null === $before )
802 $before = __('Tags: ');
803 echo get_the_tag_list($before, $sep, $after);
804}
805
806/**
807 * Retrieve tag description.
808 *
809 * @since 2.8
810 *
811 * @param int $tag Optional. Tag ID. Will use global tag ID by default.
812 * @return string Tag description, available.
813 */
814function tag_description( $tag = 0 ) {
815 return term_description( $tag );
816}
817
818/**
819 * Retrieve term description.
820 *
821 * @since 2.8
822 *
823 * @param int $term Optional. Term ID. Will use global term ID by default.
824 * @return string Term description, available.
825 */
826function term_description( $term = 0, $taxonomy = 'post_tag' ) {
827 if ( !$term && ( is_tax() || is_tag() || is_category() ) ) {
828 global $wp_query;
829 $term = $wp_query->get_queried_object();
830 $taxonomy = $term->taxonomy;
831 $term = $term->term_id;
832 }
833 return get_term_field( 'description', $term, $taxonomy );
834}
835
836/**
837 * Retrieve the terms of the taxonomy that are attached to the post.
838 *
839 * This function can only be used within the loop.
840 *
841 * @since 2.5.0
842 *
843 * @param int $id Post ID. Is not optional.
844 * @param string $taxonomy Taxonomy name.
845 * @return array|bool False on failure. Array of term objects on success.
846 */
847function get_the_terms( $id = 0, $taxonomy ) {
848 global $post;
849
850 $id = (int) $id;
851
852 if ( ! $id && ! in_the_loop() )
853 return false; // in-the-loop function
854
855 if ( !$id )
856 $id = (int) $post->ID;
857
858 $terms = get_object_term_cache( $id, $taxonomy );
859 if ( false === $terms )
860 $terms = wp_get_object_terms( $id, $taxonomy );
861
862 if ( empty( $terms ) )
863 return false;
864
865 return $terms;
866}
867
868/**
869 * Retrieve terms as a list with specified format.
870 *
871 * @since 2.5.0
872 *
873 * @param int $id Term ID.
874 * @param string $taxonomy Taxonomy name.
875 * @param string $before Optional. Before list.
876 * @param string $sep Optional. Separate items using this.
877 * @param string $after Optional. After list.
878 * @return string
879 */
880function get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '' ) {
881 $terms = get_the_terms( $id, $taxonomy );
882
883 if ( is_wp_error( $terms ) )
884 return $terms;
885
886 if ( empty( $terms ) )
887 return false;
888
889 foreach ( $terms as $term ) {
890 $link = get_term_link( $term, $taxonomy );
891 if ( is_wp_error( $link ) )
892 return $link;
893 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';
894 }
895
896 $term_links = apply_filters( "term_links-$taxonomy", $term_links );
897
898 return $before . join( $sep, $term_links ) . $after;
899}
900
901/**
902 * Display the terms in a list.
903 *
904 * @since 2.5.0
905 *
906 * @param int $id Term ID.
907 * @param string $taxonomy Taxonomy name.
908 * @param string $before Optional. Before list.
909 * @param string $sep Optional. Separate items using this.
910 * @param string $after Optional. After list.
911 * @return null|bool False on WordPress error. Returns null when displaying.
912 */
913function the_terms( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
914 $return = get_the_term_list( $id, $taxonomy, $before, $sep, $after );
915 if ( is_wp_error( $return ) )
916 return false;
917 else
918 echo $return;
919}
920
921/**
922 * Check if the current post has any of given tags.
923 *
924 * The given tags are checked against the post's tags' term_ids, names and slugs.
925 * Tags given as integers will only be checked against the post's tags' term_ids.
926 * If no tags are given, determines if post has any tags.
927 *
928 * 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)
929 * Prior to v2.7, this function could only be used in the WordPress Loop.
930 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
931 *
932 * @since 2.6.0
933 *
934 * @uses is_object_in_term()
935 *
936 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
937 * @param int|post object Optional. Post to check instead of the current post. @since 2.7.0
938 * @return bool True if the current post has any of the the given tags (or any tag, if no tag specified).
939 */
940function has_tag( $tag = '', $_post = null ) {
941 if ( $_post ) {
942 $_post = get_post( $_post );
943 } else {
944 $_post =& $GLOBALS['post'];
945 }
946
947 if ( !$_post )
948 return false;
949
950 $r = is_object_in_term( $_post->ID, 'post_tag', $tag );
951 if ( is_wp_error( $r ) )
952 return false;
953 return $r;
954}
955
956?>
Note: See TracBrowser for help on using the repository browser.