67 * For custom taxonomies use get_the_terms(). |
69 * For custom taxonomies use get_the_terms(). |
68 * |
70 * |
69 * @since 0.71 |
71 * @since 0.71 |
70 * |
72 * |
71 * @param int $id Optional, default to current post ID. The post ID. |
73 * @param int $id Optional, default to current post ID. The post ID. |
72 * @return array Array of WP_Term objects, one for each category assigned to the post. |
74 * @return WP_Term[] Array of WP_Term objects, one for each category assigned to the post. |
73 */ |
75 */ |
74 function get_the_category( $id = false ) { |
76 function get_the_category( $id = false ) { |
75 $categories = get_the_terms( $id, 'category' ); |
77 $categories = get_the_terms( $id, 'category' ); |
76 if ( ! $categories || is_wp_error( $categories ) ) |
78 if ( ! $categories || is_wp_error( $categories ) ) { |
77 $categories = array(); |
79 $categories = array(); |
|
80 } |
78 |
81 |
79 $categories = array_values( $categories ); |
82 $categories = array_values( $categories ); |
80 |
83 |
81 foreach ( array_keys( $categories ) as $key ) { |
84 foreach ( array_keys( $categories ) as $key ) { |
82 _make_cat_compat( $categories[$key] ); |
85 _make_cat_compat( $categories[ $key ] ); |
83 } |
86 } |
84 |
87 |
85 /** |
88 /** |
86 * Filters the array of categories to return for a post. |
89 * Filters the array of categories to return for a post. |
87 * |
90 * |
88 * @since 3.1.0 |
91 * @since 3.1.0 |
89 * @since 4.4.0 Added `$id` parameter. |
92 * @since 4.4.0 Added `$id` parameter. |
90 * |
93 * |
91 * @param array $categories An array of categories to return for the post. |
94 * @param WP_Term[] $categories An array of categories to return for the post. |
92 * @param int $id ID of the post. |
95 * @param int|false $id ID of the post. |
93 */ |
96 */ |
94 return apply_filters( 'get_the_categories', $categories, $id ); |
97 return apply_filters( 'get_the_categories', $categories, $id ); |
95 } |
98 } |
96 |
99 |
97 /** |
100 /** |
135 /** |
139 /** |
136 * Filters the categories before building the category list. |
140 * Filters the categories before building the category list. |
137 * |
141 * |
138 * @since 4.4.0 |
142 * @since 4.4.0 |
139 * |
143 * |
140 * @param array $categories An array of the post's categories. |
144 * @param WP_Term[] $categories An array of the post's categories. |
141 * @param int|bool $post_id ID of the post we're retrieving categories for. When `false`, we assume the |
145 * @param int|bool $post_id ID of the post we're retrieving categories for. When `false`, we assume the |
142 * current post in the loop. |
146 * current post in the loop. |
143 */ |
147 */ |
144 $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id ); |
148 $categories = apply_filters( 'the_category_list', get_the_category( $post_id ), $post_id ); |
145 |
149 |
146 if ( empty( $categories ) ) { |
150 if ( empty( $categories ) ) { |
147 /** This filter is documented in wp-includes/category-template.php */ |
151 /** This filter is documented in wp-includes/category-template.php */ |
155 $thelist .= '<ul class="post-categories">'; |
159 $thelist .= '<ul class="post-categories">'; |
156 foreach ( $categories as $category ) { |
160 foreach ( $categories as $category ) { |
157 $thelist .= "\n\t<li>"; |
161 $thelist .= "\n\t<li>"; |
158 switch ( strtolower( $parents ) ) { |
162 switch ( strtolower( $parents ) ) { |
159 case 'multiple': |
163 case 'multiple': |
160 if ( $category->parent ) |
164 if ( $category->parent ) { |
161 $thelist .= get_category_parents( $category->parent, true, $separator ); |
165 $thelist .= get_category_parents( $category->parent, true, $separator ); |
162 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>'; |
166 } |
|
167 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; |
163 break; |
168 break; |
164 case 'single': |
169 case 'single': |
165 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
170 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
166 if ( $category->parent ) |
171 if ( $category->parent ) { |
167 $thelist .= get_category_parents( $category->parent, false, $separator ); |
172 $thelist .= get_category_parents( $category->parent, false, $separator ); |
168 $thelist .= $category->name.'</a></li>'; |
173 } |
|
174 $thelist .= $category->name . '</a></li>'; |
169 break; |
175 break; |
170 case '': |
176 case '': |
171 default: |
177 default: |
172 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a></li>'; |
178 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a></li>'; |
173 } |
179 } |
174 } |
180 } |
175 $thelist .= '</ul>'; |
181 $thelist .= '</ul>'; |
176 } else { |
182 } else { |
177 $i = 0; |
183 $i = 0; |
178 foreach ( $categories as $category ) { |
184 foreach ( $categories as $category ) { |
179 if ( 0 < $i ) |
185 if ( 0 < $i ) { |
180 $thelist .= $separator; |
186 $thelist .= $separator; |
|
187 } |
181 switch ( strtolower( $parents ) ) { |
188 switch ( strtolower( $parents ) ) { |
182 case 'multiple': |
189 case 'multiple': |
183 if ( $category->parent ) |
190 if ( $category->parent ) { |
184 $thelist .= get_category_parents( $category->parent, true, $separator ); |
191 $thelist .= get_category_parents( $category->parent, true, $separator ); |
185 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>'; |
192 } |
|
193 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; |
186 break; |
194 break; |
187 case 'single': |
195 case 'single': |
188 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
196 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>'; |
189 if ( $category->parent ) |
197 if ( $category->parent ) { |
190 $thelist .= get_category_parents( $category->parent, false, $separator ); |
198 $thelist .= get_category_parents( $category->parent, false, $separator ); |
|
199 } |
191 $thelist .= "$category->name</a>"; |
200 $thelist .= "$category->name</a>"; |
192 break; |
201 break; |
193 case '': |
202 case '': |
194 default: |
203 default: |
195 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name.'</a>'; |
204 $thelist .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" ' . $rel . '>' . $category->name . '</a>'; |
196 } |
205 } |
197 ++$i; |
206 ++$i; |
198 } |
207 } |
199 } |
208 } |
200 |
209 |
210 */ |
219 */ |
211 return apply_filters( 'the_category', $thelist, $separator, $parents ); |
220 return apply_filters( 'the_category', $thelist, $separator, $parents ); |
212 } |
221 } |
213 |
222 |
214 /** |
223 /** |
215 * Check if the current post is within any of the given categories. |
224 * Checks if the current post is within any of the given categories. |
216 * |
225 * |
217 * The given categories are checked against the post's categories' term_ids, names and slugs. |
226 * The given categories are checked against the post's categories' term_ids, names and slugs. |
218 * Categories given as integers will only be checked against the post's categories' term_ids. |
227 * Categories given as integers will only be checked against the post's categories' term_ids. |
219 * |
228 * |
220 * Prior to v2.5 of WordPress, category names were not supported. |
229 * Prior to v2.5 of WordPress, category names were not supported. |
221 * Prior to v2.7, category slugs were not supported. |
230 * Prior to v2.7, category slugs were not supported. |
222 * Prior to v2.7, only one category could be compared: in_category( $single_category ). |
231 * Prior to v2.7, only one category could be compared: in_category( $single_category ). |
223 * Prior to v2.7, this function could only be used in the WordPress Loop. |
232 * Prior to v2.7, this function could only be used in the WordPress Loop. |
224 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
233 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
225 * |
234 * |
|
235 * For more information on this and similar theme functions, check out |
|
236 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
237 * Conditional Tags} article in the Theme Developer Handbook. |
|
238 * |
226 * @since 1.2.0 |
239 * @since 1.2.0 |
227 * |
240 * |
228 * @param int|string|array $category Category ID, name or slug, or array of said. |
241 * @param int|string|array $category Category ID, name or slug, or array of said. |
229 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) |
242 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) |
230 * @return bool True if the current post is in any of the given categories. |
243 * @return bool True if the current post is in any of the given categories. |
231 */ |
244 */ |
232 function in_category( $category, $post = null ) { |
245 function in_category( $category, $post = null ) { |
233 if ( empty( $category ) ) |
246 if ( empty( $category ) ) { |
234 return false; |
247 return false; |
|
248 } |
235 |
249 |
236 return has_category( $category, $post ); |
250 return has_category( $category, $post ); |
237 } |
251 } |
238 |
252 |
239 /** |
253 /** |
336 |
350 |
337 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
351 $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0; |
338 |
352 |
339 // Back compat. |
353 // Back compat. |
340 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
354 if ( isset( $args['type'] ) && 'link' == $args['type'] ) { |
341 _deprecated_argument( __FUNCTION__, '3.0.0', |
355 _deprecated_argument( |
|
356 __FUNCTION__, |
|
357 '3.0.0', |
342 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
358 /* translators: 1: "type => link", 2: "taxonomy => link_category" */ |
343 sprintf( __( '%1$s is deprecated. Use %2$s instead.' ), |
359 sprintf( |
|
360 __( '%1$s is deprecated. Use %2$s instead.' ), |
344 '<code>type => link</code>', |
361 '<code>type => link</code>', |
345 '<code>taxonomy => link_category</code>' |
362 '<code>taxonomy => link_category</code>' |
346 ) |
363 ) |
347 ); |
364 ); |
348 $args['taxonomy'] = 'link_category'; |
365 $args['taxonomy'] = 'link_category'; |
349 } |
366 } |
350 |
367 |
351 $r = wp_parse_args( $args, $defaults ); |
368 $r = wp_parse_args( $args, $defaults ); |
352 $option_none_value = $r['option_none_value']; |
369 $option_none_value = $r['option_none_value']; |
353 |
370 |
354 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) { |
371 if ( ! isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) { |
355 $r['pad_counts'] = true; |
372 $r['pad_counts'] = true; |
356 } |
373 } |
365 // Avoid clashes with the 'name' param of get_terms(). |
382 // Avoid clashes with the 'name' param of get_terms(). |
366 $get_terms_args = $r; |
383 $get_terms_args = $r; |
367 unset( $get_terms_args['name'] ); |
384 unset( $get_terms_args['name'] ); |
368 $categories = get_terms( $r['taxonomy'], $get_terms_args ); |
385 $categories = get_terms( $r['taxonomy'], $get_terms_args ); |
369 |
386 |
370 $name = esc_attr( $r['name'] ); |
387 $name = esc_attr( $r['name'] ); |
371 $class = esc_attr( $r['class'] ); |
388 $class = esc_attr( $r['class'] ); |
372 $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
389 $id = $r['id'] ? esc_attr( $r['id'] ) : $name; |
373 $required = $r['required'] ? 'required' : ''; |
390 $required = $r['required'] ? 'required' : ''; |
374 |
391 |
375 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
392 if ( ! $r['hide_if_empty'] || ! empty( $categories ) ) { |
376 $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
393 $output = "<select $required name='$name' id='$id' class='$class' $tab_index_attribute>\n"; |
377 } else { |
394 } else { |
393 * |
410 * |
394 * @param string $element Category name. |
411 * @param string $element Category name. |
395 * @param WP_Term|null $category The category object, or null if there's no corresponding category. |
412 * @param WP_Term|null $category The category object, or null if there's no corresponding category. |
396 */ |
413 */ |
397 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
414 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
398 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
415 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "' selected='selected'>$show_option_none</option>\n"; |
399 } |
416 } |
400 |
417 |
401 if ( ! empty( $categories ) ) { |
418 if ( ! empty( $categories ) ) { |
402 |
419 |
403 if ( $r['show_option_all'] ) { |
420 if ( $r['show_option_all'] ) { |
404 |
421 |
405 /** This filter is documented in wp-includes/category-template.php */ |
422 /** This filter is documented in wp-includes/category-template.php */ |
406 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'], null ); |
423 $show_option_all = apply_filters( 'list_cats', $r['show_option_all'], null ); |
407 $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : ''; |
424 $selected = ( '0' === strval( $r['selected'] ) ) ? " selected='selected'" : ''; |
408 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
425 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
409 } |
426 } |
410 |
427 |
411 if ( $r['show_option_none'] ) { |
428 if ( $r['show_option_none'] ) { |
412 |
429 |
413 /** This filter is documented in wp-includes/category-template.php */ |
430 /** This filter is documented in wp-includes/category-template.php */ |
414 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
431 $show_option_none = apply_filters( 'list_cats', $r['show_option_none'], null ); |
415 $selected = selected( $option_none_value, $r['selected'], false ); |
432 $selected = selected( $option_none_value, $r['selected'], false ); |
416 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
433 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$selected>$show_option_none</option>\n"; |
417 } |
434 } |
418 |
435 |
419 if ( $r['hierarchical'] ) { |
436 if ( $r['hierarchical'] ) { |
420 $depth = $r['depth']; // Walk the full depth. |
437 $depth = $r['depth']; // Walk the full depth. |
421 } else { |
438 } else { |
536 if ( $r['exclude'] ) { |
554 if ( $r['exclude'] ) { |
537 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) ); |
555 $exclude_tree = array_merge( $exclude_tree, wp_parse_id_list( $r['exclude'] ) ); |
538 } |
556 } |
539 |
557 |
540 $r['exclude_tree'] = $exclude_tree; |
558 $r['exclude_tree'] = $exclude_tree; |
541 $r['exclude'] = ''; |
559 $r['exclude'] = ''; |
542 } |
560 } |
543 |
561 |
544 if ( ! isset( $r['class'] ) ) |
562 if ( ! isset( $r['class'] ) ) { |
545 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; |
563 $r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy']; |
|
564 } |
546 |
565 |
547 if ( ! taxonomy_exists( $r['taxonomy'] ) ) { |
566 if ( ! taxonomy_exists( $r['taxonomy'] ) ) { |
548 return false; |
567 return false; |
549 } |
568 } |
550 |
569 |
551 $show_option_all = $r['show_option_all']; |
570 $show_option_all = $r['show_option_all']; |
552 $show_option_none = $r['show_option_none']; |
571 $show_option_none = $r['show_option_none']; |
553 |
572 |
554 $categories = get_categories( $r ); |
573 $categories = get_categories( $r ); |
555 |
574 |
556 $output = ''; |
575 $output = ''; |
636 return $html; |
655 return $html; |
637 } |
656 } |
638 } |
657 } |
639 |
658 |
640 /** |
659 /** |
641 * Display tag cloud. |
660 * Displays a tag cloud. |
642 * |
|
643 * The text size is set by the 'smallest' and 'largest' arguments, which will |
|
644 * use the 'unit' argument value for the CSS text size unit. The 'format' |
|
645 * argument can be 'flat' (default), 'list', or 'array'. The flat value for the |
|
646 * 'format' argument will separate tags with spaces. The list value for the |
|
647 * 'format' argument will format the tags in a UL HTML list. The array value for |
|
648 * the 'format' argument will return in PHP array type format. |
|
649 * |
|
650 * The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'. |
|
651 * The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'. |
|
652 * |
|
653 * The 'number' argument is how many tags to return. By default, the limit will |
|
654 * be to return the top 45 tags in the tag cloud list. |
|
655 * |
|
656 * The 'topic_count_text' argument is a nooped plural from _n_noop() to generate the |
|
657 * text for the tag link count. |
|
658 * |
|
659 * The 'topic_count_text_callback' argument is a function, which given the count |
|
660 * of the posts with that tag returns a text for the tag link count. |
|
661 * |
|
662 * The 'post_type' argument is used only when 'link' is set to 'edit'. It determines the post_type |
|
663 * passed to edit.php for the popular tags edit links. |
|
664 * |
|
665 * The 'exclude' and 'include' arguments are used for the get_tags() function. Only one |
|
666 * should be used, because only one will be used and the other ignored, if they are both set. |
|
667 * |
661 * |
668 * @since 2.3.0 |
662 * @since 2.3.0 |
669 * @since 4.8.0 Added the `show_count` argument. |
663 * @since 4.8.0 Added the `show_count` argument. |
670 * |
664 * |
671 * @param array|string|null $args Optional. Override default arguments. |
665 * @param array|string $args { |
|
666 * Optional. Array or string of arguments for displaying a tag cloud. See wp_generate_tag_cloud() |
|
667 * and get_terms() for the full lists of arguments that can be passed in `$args`. |
|
668 * |
|
669 * @type int $number The number of tags to display. Accepts any positive integer |
|
670 * or zero to return all. Default 0 (all tags). |
|
671 * @type string $link Whether to display term editing links or term permalinks. |
|
672 * Accepts 'edit' and 'view'. Default 'view'. |
|
673 * @type string $post_type The post type. Used to highlight the proper post type menu |
|
674 * on the linked edit page. Defaults to the first post type |
|
675 * associated with the taxonomy. |
|
676 * @type bool $echo Whether or not to echo the return value. Default true. |
|
677 * } |
672 * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
678 * @return void|array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument. |
673 * Otherwise, this function outputs the tag cloud. |
679 * Otherwise, this function outputs the tag cloud. |
674 */ |
680 */ |
675 function wp_tag_cloud( $args = '' ) { |
681 function wp_tag_cloud( $args = '' ) { |
676 $defaults = array( |
682 $defaults = array( |
677 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, |
683 'smallest' => 8, |
678 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
684 'largest' => 22, |
679 'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'post_type' => '', 'echo' => true, |
685 'unit' => 'pt', |
|
686 'number' => 45, |
|
687 'format' => 'flat', |
|
688 'separator' => "\n", |
|
689 'orderby' => 'name', |
|
690 'order' => 'ASC', |
|
691 'exclude' => '', |
|
692 'include' => '', |
|
693 'link' => 'view', |
|
694 'taxonomy' => 'post_tag', |
|
695 'post_type' => '', |
|
696 'echo' => true, |
680 'show_count' => 0, |
697 'show_count' => 0, |
681 ); |
698 ); |
682 $args = wp_parse_args( $args, $defaults ); |
699 $args = wp_parse_args( $args, $defaults ); |
683 |
700 |
684 $tags = get_terms( $args['taxonomy'], array_merge( $args, array( 'orderby' => 'count', 'order' => 'DESC' ) ) ); // Always query top tags |
701 $tags = get_terms( |
685 |
702 $args['taxonomy'], |
686 if ( empty( $tags ) || is_wp_error( $tags ) ) |
703 array_merge( |
|
704 $args, |
|
705 array( |
|
706 'orderby' => 'count', |
|
707 'order' => 'DESC', |
|
708 ) |
|
709 ) |
|
710 ); // Always query top tags |
|
711 |
|
712 if ( empty( $tags ) || is_wp_error( $tags ) ) { |
687 return; |
713 return; |
|
714 } |
688 |
715 |
689 foreach ( $tags as $key => $tag ) { |
716 foreach ( $tags as $key => $tag ) { |
690 if ( 'edit' == $args['link'] ) |
717 if ( 'edit' == $args['link'] ) { |
691 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
718 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
692 else |
719 } else { |
693 $link = get_term_link( intval($tag->term_id), $tag->taxonomy ); |
720 $link = get_term_link( intval( $tag->term_id ), $tag->taxonomy ); |
694 if ( is_wp_error( $link ) ) |
721 } |
|
722 if ( is_wp_error( $link ) ) { |
695 return; |
723 return; |
|
724 } |
696 |
725 |
697 $tags[ $key ]->link = $link; |
726 $tags[ $key ]->link = $link; |
698 $tags[ $key ]->id = $tag->term_id; |
727 $tags[ $key ]->id = $tag->term_id; |
699 } |
728 } |
700 |
729 |
701 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args |
730 $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args |
702 |
731 |
703 /** |
732 /** |
723 * |
753 * |
724 * @param int $count Number of posts with that tag. |
754 * @param int $count Number of posts with that tag. |
725 * @return int Scaled count. |
755 * @return int Scaled count. |
726 */ |
756 */ |
727 function default_topic_count_scale( $count ) { |
757 function default_topic_count_scale( $count ) { |
728 return round(log10($count + 1) * 100); |
758 return round( log10( $count + 1 ) * 100 ); |
729 } |
759 } |
730 |
760 |
731 /** |
761 /** |
732 * Generates a tag cloud (heatmap) from provided data. |
762 * Generates a tag cloud (heatmap) from provided data. |
733 * |
763 * |
734 * @todo Complete functionality. |
764 * @todo Complete functionality. |
735 * @since 2.3.0 |
765 * @since 2.3.0 |
736 * @since 4.8.0 Added the `show_count` argument. |
766 * @since 4.8.0 Added the `show_count` argument. |
737 * |
767 * |
738 * @param array $tags List of tags. |
768 * @param WP_Term[] $tags Array of WP_Term objects to generate the tag cloud for. |
739 * @param string|array $args { |
769 * @param string|array $args { |
740 * Optional. Array of string of arguments for generating a tag cloud. |
770 * Optional. Array or string of arguments for generating a tag cloud. |
741 * |
771 * |
742 * @type int $smallest Smallest font size used to display tags. Paired |
772 * @type int $smallest Smallest font size used to display tags. Paired |
743 * with the value of `$unit`, to determine CSS text |
773 * with the value of `$unit`, to determine CSS text |
744 * size unit. Default 8 (pt). |
774 * size unit. Default 8 (pt). |
745 * @type int $largest Largest font size used to display tags. Paired |
775 * @type int $largest Largest font size used to display tags. Paired |
774 * } |
804 * } |
775 * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
805 * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
776 */ |
806 */ |
777 function wp_generate_tag_cloud( $tags, $args = '' ) { |
807 function wp_generate_tag_cloud( $tags, $args = '' ) { |
778 $defaults = array( |
808 $defaults = array( |
779 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, |
809 'smallest' => 8, |
780 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC', |
810 'largest' => 22, |
781 'topic_count_text' => null, 'topic_count_text_callback' => null, |
811 'unit' => 'pt', |
782 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1, |
812 'number' => 0, |
783 'show_count' => 0, |
813 'format' => 'flat', |
|
814 'separator' => "\n", |
|
815 'orderby' => 'name', |
|
816 'order' => 'ASC', |
|
817 'topic_count_text' => null, |
|
818 'topic_count_text_callback' => null, |
|
819 'topic_count_scale_callback' => 'default_topic_count_scale', |
|
820 'filter' => 1, |
|
821 'show_count' => 0, |
784 ); |
822 ); |
785 |
823 |
786 $args = wp_parse_args( $args, $defaults ); |
824 $args = wp_parse_args( $args, $defaults ); |
787 |
825 |
788 $return = ( 'array' === $args['format'] ) ? array() : ''; |
826 $return = ( 'array' === $args['format'] ) ? array() : ''; |
802 } else { |
840 } else { |
803 $translate_nooped_plural = false; |
841 $translate_nooped_plural = false; |
804 } |
842 } |
805 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
843 } elseif ( isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) { |
806 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
844 // If no callback exists, look for the old-style single_text and multiple_text arguments. |
|
845 // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
807 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
846 $translate_nooped_plural = _n_noop( $args['single_text'], $args['multiple_text'] ); |
808 } else { |
847 } else { |
809 // This is the default for when no callback, plural, or argument is passed in. |
848 // This is the default for when no callback, plural, or argument is passed in. |
810 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
849 $translate_nooped_plural = _n_noop( '%s item', '%s items' ); |
811 } |
850 } |
841 $tags = array_reverse( $tags, true ); |
880 $tags = array_reverse( $tags, true ); |
842 } |
881 } |
843 } |
882 } |
844 } |
883 } |
845 |
884 |
846 if ( $args['number'] > 0 ) |
885 if ( $args['number'] > 0 ) { |
847 $tags = array_slice( $tags, 0, $args['number'] ); |
886 $tags = array_slice( $tags, 0, $args['number'] ); |
848 |
887 } |
849 $counts = array(); |
888 |
|
889 $counts = array(); |
850 $real_counts = array(); // For the alt tag |
890 $real_counts = array(); // For the alt tag |
851 foreach ( (array) $tags as $key => $tag ) { |
891 foreach ( (array) $tags as $key => $tag ) { |
852 $real_counts[ $key ] = $tag->count; |
892 $real_counts[ $key ] = $tag->count; |
853 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count ); |
893 $counts[ $key ] = call_user_func( $args['topic_count_scale_callback'], $tag->count ); |
854 } |
894 } |
855 |
895 |
856 $min_count = min( $counts ); |
896 $min_count = min( $counts ); |
857 $spread = max( $counts ) - $min_count; |
897 $spread = max( $counts ) - $min_count; |
858 if ( $spread <= 0 ) |
898 if ( $spread <= 0 ) { |
859 $spread = 1; |
899 $spread = 1; |
|
900 } |
860 $font_spread = $args['largest'] - $args['smallest']; |
901 $font_spread = $args['largest'] - $args['smallest']; |
861 if ( $font_spread < 0 ) |
902 if ( $font_spread < 0 ) { |
862 $font_spread = 1; |
903 $font_spread = 1; |
|
904 } |
863 $font_step = $font_spread / $spread; |
905 $font_step = $font_spread / $spread; |
864 |
906 |
865 $aria_label = false; |
907 $aria_label = false; |
866 /* |
908 /* |
867 * Determine whether to output an 'aria-label' attribute with the tag name and count. |
909 * Determine whether to output an 'aria-label' attribute with the tag name and count. |
881 // Assemble the data that will be used to generate the tag cloud markup. |
923 // Assemble the data that will be used to generate the tag cloud markup. |
882 $tags_data = array(); |
924 $tags_data = array(); |
883 foreach ( $tags as $key => $tag ) { |
925 foreach ( $tags as $key => $tag ) { |
884 $tag_id = isset( $tag->id ) ? $tag->id : $key; |
926 $tag_id = isset( $tag->id ) ? $tag->id : $key; |
885 |
927 |
886 $count = $counts[ $key ]; |
928 $count = $counts[ $key ]; |
887 $real_count = $real_counts[ $key ]; |
929 $real_count = $real_counts[ $key ]; |
888 |
930 |
889 if ( $translate_nooped_plural ) { |
931 if ( $translate_nooped_plural ) { |
890 $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
932 $formatted_count = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) ); |
891 } else { |
933 } else { |
919 $a = array(); |
961 $a = array(); |
920 |
962 |
921 // Generate the output links array. |
963 // Generate the output links array. |
922 foreach ( $tags_data as $key => $tag_data ) { |
964 foreach ( $tags_data as $key => $tag_data ) { |
923 $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); |
965 $class = $tag_data['class'] . ' tag-link-position-' . ( $key + 1 ); |
924 $a[] = sprintf( |
966 $a[] = sprintf( |
925 '<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', |
967 '<a href="%1$s"%2$s class="%3$s" style="font-size: %4$s;"%5$s>%6$s%7$s</a>', |
926 esc_url( $tag_data['url'] ), |
968 esc_url( $tag_data['url'] ), |
927 $tag_data['role'], |
969 $tag_data['role'], |
928 esc_attr( $class ), |
970 esc_attr( $class ), |
929 esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), |
971 esc_attr( str_replace( ',', '.', $tag_data['font_size'] ) . $args['unit'] ), |
932 $tag_data['show_count'] |
974 $tag_data['show_count'] |
933 ); |
975 ); |
934 } |
976 } |
935 |
977 |
936 switch ( $args['format'] ) { |
978 switch ( $args['format'] ) { |
937 case 'array' : |
979 case 'array': |
938 $return =& $a; |
980 $return =& $a; |
939 break; |
981 break; |
940 case 'list' : |
982 case 'list': |
941 /* |
983 /* |
942 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
984 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
943 * technologies the default role when the list is styled with `list-style: none`. |
985 * technologies the default role when the list is styled with `list-style: none`. |
944 * Note: this is redundant but doesn't harm. |
986 * Note: this is redundant but doesn't harm. |
945 */ |
987 */ |
946 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
988 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
947 $return .= join( "</li>\n\t<li>", $a ); |
989 $return .= join( "</li>\n\t<li>", $a ); |
948 $return .= "</li>\n</ul>\n"; |
990 $return .= "</li>\n</ul>\n"; |
949 break; |
991 break; |
950 default : |
992 default: |
951 $return = join( $args['separator'], $a ); |
993 $return = join( $args['separator'], $a ); |
952 break; |
994 break; |
953 } |
995 } |
954 |
996 |
955 if ( $args['filter'] ) { |
997 if ( $args['filter'] ) { |
1125 * @param string $before Optional. Before list. |
1166 * @param string $before Optional. Before list. |
1126 * @param string $sep Optional. Separate items using this. |
1167 * @param string $sep Optional. Separate items using this. |
1127 * @param string $after Optional. After list. |
1168 * @param string $after Optional. After list. |
1128 */ |
1169 */ |
1129 function the_tags( $before = null, $sep = ', ', $after = '' ) { |
1170 function the_tags( $before = null, $sep = ', ', $after = '' ) { |
1130 if ( null === $before ) |
1171 if ( null === $before ) { |
1131 $before = __('Tags: '); |
1172 $before = __( 'Tags: ' ); |
|
1173 } |
1132 |
1174 |
1133 $the_tags = get_the_tag_list( $before, $sep, $after ); |
1175 $the_tags = get_the_tag_list( $before, $sep, $after ); |
1134 |
1176 |
1135 if ( ! is_wp_error( $the_tags ) ) { |
1177 if ( ! is_wp_error( $the_tags ) ) { |
1136 echo $the_tags; |
1178 echo $the_tags; |
1173 /** |
1215 /** |
1174 * Retrieve the terms of the taxonomy that are attached to the post. |
1216 * Retrieve the terms of the taxonomy that are attached to the post. |
1175 * |
1217 * |
1176 * @since 2.5.0 |
1218 * @since 2.5.0 |
1177 * |
1219 * |
1178 * @param int|object $post Post ID or object. |
1220 * @param int|WP_Post $post Post ID or object. |
1179 * @param string $taxonomy Taxonomy name. |
1221 * @param string $taxonomy Taxonomy name. |
1180 * @return array|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
1222 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
1181 * or the post does not exist, WP_Error on failure. |
1223 * or the post does not exist, WP_Error on failure. |
1182 */ |
1224 */ |
1183 function get_the_terms( $post, $taxonomy ) { |
1225 function get_the_terms( $post, $taxonomy ) { |
1184 if ( ! $post = get_post( $post ) ) |
1226 if ( ! $post = get_post( $post ) ) { |
1185 return false; |
1227 return false; |
|
1228 } |
1186 |
1229 |
1187 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1230 $terms = get_object_term_cache( $post->ID, $taxonomy ); |
1188 if ( false === $terms ) { |
1231 if ( false === $terms ) { |
1189 $terms = wp_get_object_terms( $post->ID, $taxonomy ); |
1232 $terms = wp_get_object_terms( $post->ID, $taxonomy ); |
1190 if ( ! is_wp_error( $terms ) ) { |
1233 if ( ! is_wp_error( $terms ) ) { |
1223 * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure. |
1267 * @return string|false|WP_Error A list of terms on success, false if there are no terms, WP_Error on failure. |
1224 */ |
1268 */ |
1225 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { |
1269 function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) { |
1226 $terms = get_the_terms( $id, $taxonomy ); |
1270 $terms = get_the_terms( $id, $taxonomy ); |
1227 |
1271 |
1228 if ( is_wp_error( $terms ) ) |
1272 if ( is_wp_error( $terms ) ) { |
1229 return $terms; |
1273 return $terms; |
1230 |
1274 } |
1231 if ( empty( $terms ) ) |
1275 |
|
1276 if ( empty( $terms ) ) { |
1232 return false; |
1277 return false; |
|
1278 } |
1233 |
1279 |
1234 $links = array(); |
1280 $links = array(); |
1235 |
1281 |
1236 foreach ( $terms as $term ) { |
1282 foreach ( $terms as $term ) { |
1237 $link = get_term_link( $term, $taxonomy ); |
1283 $link = get_term_link( $term, $taxonomy ); |
1334 * @return false|void False on WordPress error. |
1380 * @return false|void False on WordPress error. |
1335 */ |
1381 */ |
1336 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { |
1382 function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { |
1337 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); |
1383 $term_list = get_the_term_list( $id, $taxonomy, $before, $sep, $after ); |
1338 |
1384 |
1339 if ( is_wp_error( $term_list ) ) |
1385 if ( is_wp_error( $term_list ) ) { |
1340 return false; |
1386 return false; |
|
1387 } |
1341 |
1388 |
1342 /** |
1389 /** |
1343 * Filters the list of terms to display. |
1390 * Filters the list of terms to display. |
1344 * |
1391 * |
1345 * @since 2.9.0 |
1392 * @since 2.9.0 |
1346 * |
1393 * |
1347 * @param array $term_list List of terms to display. |
1394 * @param string $term_list List of terms to display. |
1348 * @param string $taxonomy The taxonomy name. |
1395 * @param string $taxonomy The taxonomy name. |
1349 * @param string $before String to use before the terms. |
1396 * @param string $before String to use before the terms. |
1350 * @param string $sep String to use between the terms. |
1397 * @param string $sep String to use between the terms. |
1351 * @param string $after String to use after the terms. |
1398 * @param string $after String to use after the terms. |
1352 */ |
1399 */ |
1365 function has_category( $category = '', $post = null ) { |
1412 function has_category( $category = '', $post = null ) { |
1366 return has_term( $category, 'category', $post ); |
1413 return has_term( $category, 'category', $post ); |
1367 } |
1414 } |
1368 |
1415 |
1369 /** |
1416 /** |
1370 * Check if the current post has any of given tags. |
1417 * Checks if the current post has any of given tags. |
1371 * |
1418 * |
1372 * The given tags are checked against the post's tags' term_ids, names and slugs. |
1419 * The given tags are checked against the post's tags' term_ids, names and slugs. |
1373 * Tags given as integers will only be checked against the post's tags' term_ids. |
1420 * Tags given as integers will only be checked against the post's tags' term_ids. |
1374 * If no tags are given, determines if post has any tags. |
1421 * If no tags are given, determines if post has any tags. |
1375 * |
1422 * |
1376 * 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) |
1423 * 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) |
1377 * Prior to v2.7, this function could only be used in the WordPress Loop. |
1424 * Prior to v2.7, this function could only be used in the WordPress Loop. |
1378 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
1425 * As of 2.7, the function can be used anywhere if it is provided a post ID or post object. |
|
1426 * |
|
1427 * For more information on this and similar theme functions, check out |
|
1428 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1429 * Conditional Tags} article in the Theme Developer Handbook. |
1379 * |
1430 * |
1380 * @since 2.6.0 |
1431 * @since 2.6.0 |
1381 * |
1432 * |
1382 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. |
1433 * @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for. |
1383 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) |
1434 * @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0) |
1400 * @param string $taxonomy Taxonomy name |
1451 * @param string $taxonomy Taxonomy name |
1401 * @param int|object $post Optional. Post to check instead of the current post. |
1452 * @param int|object $post Optional. Post to check instead of the current post. |
1402 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). |
1453 * @return bool True if the current post has any of the given tags (or any tag, if no tag specified). |
1403 */ |
1454 */ |
1404 function has_term( $term = '', $taxonomy = '', $post = null ) { |
1455 function has_term( $term = '', $taxonomy = '', $post = null ) { |
1405 $post = get_post($post); |
1456 $post = get_post( $post ); |
1406 |
1457 |
1407 if ( !$post ) |
1458 if ( ! $post ) { |
1408 return false; |
1459 return false; |
|
1460 } |
1409 |
1461 |
1410 $r = is_object_in_term( $post->ID, $taxonomy, $term ); |
1462 $r = is_object_in_term( $post->ID, $taxonomy, $term ); |
1411 if ( is_wp_error( $r ) ) |
1463 if ( is_wp_error( $r ) ) { |
1412 return false; |
1464 return false; |
|
1465 } |
1413 |
1466 |
1414 return $r; |
1467 return $r; |
1415 } |
1468 } |