246 * Conditional Tags} article in the Theme Developer Handbook. |
246 * Conditional Tags} article in the Theme Developer Handbook. |
247 * |
247 * |
248 * @since 1.2.0 |
248 * @since 1.2.0 |
249 * @since 2.7.0 The `$post` parameter was added. |
249 * @since 2.7.0 The `$post` parameter was added. |
250 * |
250 * |
251 * @param int|string|array $category Category ID, name or slug, or array of said. |
251 * @param int|string|int[]|string[] $category Category ID, name, slug, or array of such |
252 * @param int|object $post Optional. Post to check instead of the current post. |
252 * to check against. |
|
253 * @param int|object $post Optional. Post to check instead of the current post. |
253 * @return bool True if the current post is in any of the given categories. |
254 * @return bool True if the current post is in any of the given categories. |
254 */ |
255 */ |
255 function in_category( $category, $post = null ) { |
256 function in_category( $category, $post = null ) { |
256 if ( empty( $category ) ) { |
257 if ( empty( $category ) ) { |
257 return false; |
258 return false; |
323 * @type int|string $selected Value of the option that should be selected. Default 0. |
324 * @type int|string $selected Value of the option that should be selected. Default 0. |
324 * @type string $value_field Term field that should be used to populate the 'value' attribute |
325 * @type string $value_field Term field that should be used to populate the 'value' attribute |
325 * of the option elements. Accepts any valid term field: 'term_id', 'name', |
326 * of the option elements. Accepts any valid term field: 'term_id', 'name', |
326 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', |
327 * 'slug', 'term_group', 'term_taxonomy_id', 'taxonomy', 'description', |
327 * 'parent', 'count'. Default 'term_id'. |
328 * 'parent', 'count'. Default 'term_id'. |
328 * @type string|array $taxonomy Name of the category or categories to retrieve. Default 'category'. |
329 * @type string|array $taxonomy Name of the taxonomy or taxonomies to retrieve. Default 'category'. |
329 * @type bool $hide_if_empty True to skip generating markup if no categories are found. |
330 * @type bool $hide_if_empty True to skip generating markup if no categories are found. |
330 * Default false (create select element even if no categories are found). |
331 * Default false (create select element even if no categories are found). |
331 * @type bool $required Whether the `<select>` element should have the HTML5 'required' attribute. |
332 * @type bool $required Whether the `<select>` element should have the HTML5 'required' attribute. |
332 * Default false. |
333 * Default false. |
333 * } |
334 * } |
431 |
432 |
432 if ( $parsed_args['show_option_all'] ) { |
433 if ( $parsed_args['show_option_all'] ) { |
433 |
434 |
434 /** This filter is documented in wp-includes/category-template.php */ |
435 /** This filter is documented in wp-includes/category-template.php */ |
435 $show_option_all = apply_filters( 'list_cats', $parsed_args['show_option_all'], null ); |
436 $show_option_all = apply_filters( 'list_cats', $parsed_args['show_option_all'], null ); |
436 $selected = ( '0' === strval( $parsed_args['selected'] ) ) ? " selected='selected'" : ''; |
437 $selected = ( '0' === (string) $parsed_args['selected'] ) ? " selected='selected'" : ''; |
437 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
438 $output .= "\t<option value='0'$selected>$show_option_all</option>\n"; |
438 } |
439 } |
439 |
440 |
440 if ( $parsed_args['show_option_none'] ) { |
441 if ( $parsed_args['show_option_none'] ) { |
441 |
442 |
483 * |
484 * |
484 * @param array|string $args { |
485 * @param array|string $args { |
485 * Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() |
486 * Array of optional arguments. See get_categories(), get_terms(), and WP_Term_Query::__construct() |
486 * for information on additional accepted arguments. |
487 * for information on additional accepted arguments. |
487 * |
488 * |
488 * @type int|array $current_category ID of category, or array of IDs of categories, that should get the |
489 * @type int|int[] $current_category ID of category, or array of IDs of categories, that should get the |
489 * 'current-cat' class. Default 0. |
490 * 'current-cat' class. Default 0. |
490 * @type int $depth Category depth. Used for tab indentation. Default 0. |
491 * @type int $depth Category depth. Used for tab indentation. Default 0. |
491 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
492 * @type bool|int $echo Whether to echo or return the generated markup. Accepts 0, 1, or their |
492 * bool equivalents. Default 1. |
493 * bool equivalents. Default 1. |
493 * @type array|string $exclude Array or comma/space-separated string of term IDs to exclude. |
494 * @type int[]|string $exclude Array or comma/space-separated string of term IDs to exclude. |
494 * If `$hierarchical` is true, descendants of `$exclude` terms will also |
495 * If `$hierarchical` is true, descendants of `$exclude` terms will also |
495 * be excluded; see `$exclude_tree`. See get_terms(). |
496 * be excluded; see `$exclude_tree`. See get_terms(). |
496 * Default empty string. |
497 * Default empty string. |
497 * @type array|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
498 * @type int[]|string $exclude_tree Array or comma/space-separated string of term IDs to exclude, along |
498 * with their descendants. See get_terms(). Default empty string. |
499 * with their descendants. See get_terms(). Default empty string. |
499 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
500 * @type string $feed Text to use for the feed link. Default 'Feed for all posts filed |
500 * under [cat name]'. |
501 * under [cat name]'. |
501 * @type string $feed_image URL of an image to use for the feed link. Default empty string. |
502 * @type string $feed_image URL of an image to use for the feed link. Default empty string. |
502 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
503 * @type string $feed_type Feed type. Used to build feed link. See get_term_feed_link(). |
510 * @type string $show_option_none Text to display for the 'no categories' option. |
511 * @type string $show_option_none Text to display for the 'no categories' option. |
511 * Default 'No categories'. |
512 * Default 'No categories'. |
512 * @type string $style The style used to display the categories list. If 'list', categories |
513 * @type string $style The style used to display the categories list. If 'list', categories |
513 * will be output as an unordered list. If left empty or another value, |
514 * will be output as an unordered list. If left empty or another value, |
514 * categories will be output separated by `<br>` tags. Default 'list'. |
515 * categories will be output separated by `<br>` tags. Default 'list'. |
|
516 * @type string $taxonomy Name of the taxonomy to retrieve. Default 'category'. |
515 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
517 * @type string $title_li Text to use for the list title `<li>` element. Pass an empty string |
516 * to disable. Default 'Categories'. |
518 * to disable. Default 'Categories'. |
517 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
519 * @type bool|int $use_desc_for_title Whether to use the category description as the title attribute. |
518 * Accepts 0, 1, or their bool equivalents. Default 1. |
520 * Accepts 0, 1, or their bool equivalents. Default 1. |
519 * } |
521 * } |
658 * Filters the HTML output of a taxonomy list. |
660 * Filters the HTML output of a taxonomy list. |
659 * |
661 * |
660 * @since 2.1.0 |
662 * @since 2.1.0 |
661 * |
663 * |
662 * @param string $output HTML output. |
664 * @param string $output HTML output. |
663 * @param array $args An array of taxonomy-listing arguments. |
665 * @param array $args An array of taxonomy-listing arguments. See wp_list_categories() |
|
666 * for information on accepted arguments. |
664 */ |
667 */ |
665 $html = apply_filters( 'wp_list_categories', $output, $args ); |
668 $html = apply_filters( 'wp_list_categories', $output, $args ); |
666 |
669 |
667 if ( $parsed_args['echo'] ) { |
670 if ( $parsed_args['echo'] ) { |
668 echo $html; |
671 echo $html; |
684 * @param array|string $args { |
687 * @param array|string $args { |
685 * Optional. Array or string of arguments for displaying a tag cloud. See wp_generate_tag_cloud() |
688 * Optional. Array or string of arguments for displaying a tag cloud. See wp_generate_tag_cloud() |
686 * and get_terms() for the full lists of arguments that can be passed in `$args`. |
689 * and get_terms() for the full lists of arguments that can be passed in `$args`. |
687 * |
690 * |
688 * @type int $number The number of tags to display. Accepts any positive integer |
691 * @type int $number The number of tags to display. Accepts any positive integer |
689 * or zero to return all. Default 0 (all tags). |
692 * or zero to return all. Default 45. |
690 * @type string $link Whether to display term editing links or term permalinks. |
693 * @type string $link Whether to display term editing links or term permalinks. |
691 * Accepts 'edit' and 'view'. Default 'view'. |
694 * Accepts 'edit' and 'view'. Default 'view'. |
692 * @type string $post_type The post type. Used to highlight the proper post type menu |
695 * @type string $post_type The post type. Used to highlight the proper post type menu |
693 * on the linked edit page. Defaults to the first post type |
696 * on the linked edit page. Defaults to the first post type |
694 * associated with the taxonomy. |
697 * associated with the taxonomy. |
695 * @type bool $echo Whether or not to echo the return value. Default true. |
698 * @type bool $echo Whether or not to echo the return value. Default true. |
696 * } |
699 * } |
697 * @return void|string|array Void if 'echo' argument is true, or on failure. Otherwise, tag cloud |
700 * @return void|string|string[] Void if 'echo' argument is true, or on failure. Otherwise, tag cloud |
698 * as a string or an array, depending on 'format' argument. |
701 * as a string or an array, depending on 'format' argument. |
699 */ |
702 */ |
700 function wp_tag_cloud( $args = '' ) { |
703 function wp_tag_cloud( $args = '' ) { |
701 $defaults = array( |
704 $defaults = array( |
702 'smallest' => 8, |
705 'smallest' => 8, |
703 'largest' => 22, |
706 'largest' => 22, |
734 |
737 |
735 foreach ( $tags as $key => $tag ) { |
738 foreach ( $tags as $key => $tag ) { |
736 if ( 'edit' === $args['link'] ) { |
739 if ( 'edit' === $args['link'] ) { |
737 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
740 $link = get_edit_term_link( $tag->term_id, $tag->taxonomy, $args['post_type'] ); |
738 } else { |
741 } else { |
739 $link = get_term_link( intval( $tag->term_id ), $tag->taxonomy ); |
742 $link = get_term_link( (int) $tag->term_id, $tag->taxonomy ); |
740 } |
743 } |
741 |
744 |
742 if ( is_wp_error( $link ) ) { |
745 if ( is_wp_error( $link ) ) { |
743 return; |
746 return; |
744 } |
747 } |
753 /** |
756 /** |
754 * Filters the tag cloud output. |
757 * Filters the tag cloud output. |
755 * |
758 * |
756 * @since 2.3.0 |
759 * @since 2.3.0 |
757 * |
760 * |
758 * @param string|array $return Tag cloud as a string or an array, depending on 'format' argument. |
761 * @param string|string[] $return Tag cloud as a string or an array, depending on 'format' argument. |
759 * @param array $args An array of tag cloud arguments. |
762 * @param array $args An array of tag cloud arguments. See wp_tag_cloud() |
|
763 * for information on accepted arguments. |
760 */ |
764 */ |
761 $return = apply_filters( 'wp_tag_cloud', $return, $args ); |
765 $return = apply_filters( 'wp_tag_cloud', $return, $args ); |
762 |
766 |
763 if ( 'array' === $args['format'] || empty( $args['echo'] ) ) { |
767 if ( 'array' === $args['format'] || empty( $args['echo'] ) ) { |
764 return $return; |
768 return $return; |
811 * Default 'name'. The {@see 'tag_cloud_sort'} filter |
815 * Default 'name'. The {@see 'tag_cloud_sort'} filter |
812 * can also affect how tags are sorted. |
816 * can also affect how tags are sorted. |
813 * @type string $order How to order the tags. Accepts 'ASC' (ascending), |
817 * @type string $order How to order the tags. Accepts 'ASC' (ascending), |
814 * 'DESC' (descending), or 'RAND' (random). Default 'ASC'. |
818 * 'DESC' (descending), or 'RAND' (random). Default 'ASC'. |
815 * @type int|bool $filter Whether to enable filtering of the final output |
819 * @type int|bool $filter Whether to enable filtering of the final output |
816 * via {@see 'wp_generate_tag_cloud'}. Default 1|true. |
820 * via {@see 'wp_generate_tag_cloud'}. Default 1. |
817 * @type string $topic_count_text Nooped plural text from _n_noop() to supply to |
821 * @type string $topic_count_text Nooped plural text from _n_noop() to supply to |
818 * tag counts. Default null. |
822 * tag counts. Default null. |
819 * @type callable $topic_count_text_callback Callback used to generate nooped plural text for |
823 * @type callable $topic_count_text_callback Callback used to generate nooped plural text for |
820 * tag counts based on the count. Default null. |
824 * tag counts based on the count. Default null. |
821 * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling |
825 * @type callable $topic_count_scale_callback Callback used to determine the tag count scaling |
822 * value. Default default_topic_count_scale(). |
826 * value. Default default_topic_count_scale(). |
823 * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts |
827 * @type bool|int $show_count Whether to display the tag counts. Default 0. Accepts |
824 * 0, 1, or their bool equivalents. |
828 * 0, 1, or their bool equivalents. |
825 * } |
829 * } |
826 * @return string|array Tag cloud as a string or an array, depending on 'format' argument. |
830 * @return string|string[] Tag cloud as a string or an array, depending on 'format' argument. |
827 */ |
831 */ |
828 function wp_generate_tag_cloud( $tags, $args = '' ) { |
832 function wp_generate_tag_cloud( $tags, $args = '' ) { |
829 $defaults = array( |
833 $defaults = array( |
830 'smallest' => 8, |
834 'smallest' => 8, |
831 'largest' => 22, |
835 'largest' => 22, |
1007 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
1011 * Force role="list", as some browsers (sic: Safari 10) don't expose to assistive |
1008 * technologies the default role when the list is styled with `list-style: none`. |
1012 * technologies the default role when the list is styled with `list-style: none`. |
1009 * Note: this is redundant but doesn't harm. |
1013 * Note: this is redundant but doesn't harm. |
1010 */ |
1014 */ |
1011 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
1015 $return = "<ul class='wp-tag-cloud' role='list'>\n\t<li>"; |
1012 $return .= join( "</li>\n\t<li>", $a ); |
1016 $return .= implode( "</li>\n\t<li>", $a ); |
1013 $return .= "</li>\n</ul>\n"; |
1017 $return .= "</li>\n</ul>\n"; |
1014 break; |
1018 break; |
1015 default: |
1019 default: |
1016 $return = join( $args['separator'], $a ); |
1020 $return = implode( $args['separator'], $a ); |
1017 break; |
1021 break; |
1018 } |
1022 } |
1019 |
1023 |
1020 if ( $args['filter'] ) { |
1024 if ( $args['filter'] ) { |
1021 /** |
1025 /** |
1026 * |
1030 * |
1027 * @since 2.3.0 |
1031 * @since 2.3.0 |
1028 * |
1032 * |
1029 * @see wp_generate_tag_cloud() |
1033 * @see wp_generate_tag_cloud() |
1030 * |
1034 * |
1031 * @param array|string $return String containing the generated HTML tag cloud output |
1035 * @param string[]|string $return String containing the generated HTML tag cloud output |
1032 * or an array of tag links if the 'format' argument |
1036 * or an array of tag links if the 'format' argument |
1033 * equals 'array'. |
1037 * equals 'array'. |
1034 * @param WP_Term[] $tags An array of terms used in the tag cloud. |
1038 * @param WP_Term[] $tags An array of terms used in the tag cloud. |
1035 * @param array $args An array of wp_generate_tag_cloud() arguments. |
1039 * @param array $args An array of wp_generate_tag_cloud() arguments. |
1036 */ |
1040 */ |
1037 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); |
1041 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args ); |
1038 } else { |
1042 } else { |
1039 return $return; |
1043 return $return; |
1040 } |
1044 } |
1150 /** |
1154 /** |
1151 * Retrieves the tags for a post. |
1155 * Retrieves the tags for a post. |
1152 * |
1156 * |
1153 * @since 2.3.0 |
1157 * @since 2.3.0 |
1154 * |
1158 * |
1155 * @param int $post_id Post ID. |
1159 * @param int|WP_Post $post_id Post ID or object. |
1156 * @return array|false|WP_Error Array of tag objects on success, false on failure. |
1160 * @return WP_Term[]|false|WP_Error Array of WP_Term objects on success, false if there are no terms |
|
1161 * or the post does not exist, WP_Error on failure. |
1157 */ |
1162 */ |
1158 function get_the_tags( $post_id = 0 ) { |
1163 function get_the_tags( $post_id = 0 ) { |
1159 $terms = get_the_terms( $post_id, 'post_tag' ); |
1164 $terms = get_the_terms( $post_id, 'post_tag' ); |
1160 |
1165 |
1161 /** |
1166 /** |
1344 * |
1350 * |
1345 * @param string[] $links An array of term links. |
1351 * @param string[] $links An array of term links. |
1346 */ |
1352 */ |
1347 $term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
1353 $term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
1348 |
1354 |
1349 return $before . join( $sep, $term_links ) . $after; |
1355 return $before . implode( $sep, $term_links ) . $after; |
1350 } |
1356 } |
1351 |
1357 |
1352 /** |
1358 /** |
1353 * Retrieves term parents with separator. |
1359 * Retrieves term parents with separator. |
1354 * |
1360 * |