axonomy Name of the taxonomy. */ $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); if ( empty( $terms ) ) { return false; } return $terms; } /** * Retrieves a post's terms as a list with specified format. * * Terms are linked to their respective term listing pages. * * @since 2.5.0 * * @param int $post_id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. String to use before the terms. Default empty. * @param string $sep Optional. String to use between the terms. Default empty. * @param string $after Optional. String to use after the terms. Default empty. * @return string|false|WP_Error A list of terms on success, false if there are no terms, * WP_Error on failure. */ function get_the_term_list( $post_id, $taxonomy, $before = '', $sep = '', $after = '' ) { $terms = get_the_terms( $post_id, $taxonomy ); if ( is_wp_error( $terms ) ) { return $terms; } if ( empty( $terms ) ) { return false; } $links = array(); foreach ( $terms as $term ) { $link = get_term_link( $term, $taxonomy ); if ( is_wp_error( $link ) ) { return $link; } $links[] = ''; } /** * Filters the term links for a given taxonomy. * * The dynamic portion of the hook name, `$taxonomy`, refers * to the taxonomy slug. * * Possible hook names include: * * - `term_links-category` * - `term_links-post_tag` * - `term_links-post_format` * * @since 2.5.0 * * @param string[] $links An array of term links. */ $term_links = apply_filters( "term_links-{$taxonomy}", $links ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores return $before . implode( $sep, $term_links ) . $after; } /** * Retrieves term parents with separator. * * @since 4.8.0 * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy name. * @param string|array $args { * Array of optional arguments. * * @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'. * Default 'name'. * @type string $separator Separator for between the terms. Default '/'. * @type bool $link Whether to format as a link. Default true. * @type bool $inclusive Include the term to get the parents for. Default true. * } * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure. */ function get_term_parents_list( $term_id, $taxonomy, $args = array() ) { $list = ''; $term = get_term( $term_id, $taxonomy ); if ( is_wp_error( $term ) ) { return $term; } if ( ! $term ) { return $list; } $term_id = $term->term_id; $defaults = array( 'format' => 'name', 'separator' => '/', 'link' => true, 'inclusive' => true, ); $args = wp_parse_args( $args, $defaults ); foreach ( array( 'link', 'inclusive' ) as $bool ) { $args[ $bool ] = wp_validate_boolean( $args[ $bool ] ); } $parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' ); if ( $args['inclusive'] ) { array_unshift( $parents, $term_id ); } foreach ( array_reverse( $parents ) as $term_id ) { $parent = get_term( $term_id, $taxonomy ); $name = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name; if ( $args['link'] ) { $list .= '' . $name . '' . $args['separator']; } else { $list .= $name . $args['separator']; } } return $list; } /** * Displays the terms for a post in a list. * * @since 2.5.0 * * @param int $post_id Post ID. * @param string $taxonomy Taxonomy name. * @param string $before Optional. String to use before the terms. Default empty. * @param string $sep Optional. String to use between the terms. Default ', '. * @param string $after Optional. String to use after the terms. Default empty. * @return void|false Void on success, false on failure. */ function the_terms( $post_id, $taxonomy, $before = '', $sep = ', ', $after = '' ) { $term_list = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after ); if ( is_wp_error( $term_list ) ) { return false; } /** * Filters the list of terms to display. * * @since 2.9.0 * * @param string $term_list List of terms to display. * @param string $taxonomy The taxonomy name. * @param string $before String to use before the terms. * @param string $sep String to use between the terms. * @param string $after String to use after the terms. */ echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after ); } /** * Checks if the current post has any of given category. * * The given categories are checked against the post's categories' term_ids, names and slugs. * Categories given as integers will only be checked against the post's categories' term_ids. * * If no categories are given, determines if post has any categories. * * @since 3.1.0 * * @param string|int|array $category Optional. The category name/term_id/slug, * or an array of them to check for. Default empty. * @param int|WP_Post $post Optional. Post to check. Defaults to the current post. * @return bool True if the current post has any of the given categories * (or any category, if no category specified). False otherwise. */ function has_category( $category = '', $post = null ) { return has_term( $category, 'category', $post ); } /** * Checks if the current post has any of given tags. * * The given tags are checked against the post's tags' term_ids, names and slugs. * Tags given as integers will only be checked against the post's tags' term_ids. * * If no tags are given, determines if post has any tags. * * For more information on this and similar theme functions, check out * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ * Conditional Tags} article in the Theme Developer Handbook. * * @since 2.6.0 * @since 2.7.0 Tags given as integers are only checked against * the post's tags' term_ids, not names or slugs. * @since 2.7.0 Can be used outside of the WordPress Loop if `$post` is provided. * * @param string|int|array $tag Optional. The tag name/term_id/slug, * or an array of them to check for. Default empty. * @param int|WP_Post $post Optional. Post to check. Defaults to the current post. * @return bool True if the current post has any of the given tags * (or any tag, if no tag specified). False otherwise. */ function has_tag( $tag = '', $post = null ) { return has_term( $tag, 'post_tag', $post ); } /** * Checks if the current post has any of given terms. * * The given terms are checked against the post's terms' term_ids, names and slugs. * Terms given as integers will only be checked against the post's terms' term_ids. * * If no terms are given, determines if post has any terms. * * @since 3.1.0 * * @param string|int|array $term Optional. The term name/term_id/slug, * or an array of them to check for. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default empty. * @param int|WP_Post $post Optional. Post to check. Defaults to the current post. * @return bool True if the current post has any of the given terms * (or any term, if no term specified). False otherwise. */ function has_term( $term = '', $taxonomy = '', $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $r = is_object_in_term( $post->ID, $taxonomy, $term ); if ( is_wp_error( $r ) ) { return false; } return $r; }