f tag does not exist.
*/
function get_tag_link( $tag ) {
return get_category_link( $tag );
}
/**
* Retrieve the tags for a post.
*
* @since 2.3.0
*
* @param int $id Post ID.
* @return array|false|WP_Error Array of tag objects on success, false on failure.
*/
function get_the_tags( $id = 0 ) {
/**
* Filters the array of tags for the given post.
*
* @since 2.3.0
*
* @see get_the_terms()
*
* @param array $terms An array of tags for the given post.
*/
return apply_filters( 'get_the_tags', get_the_terms( $id, 'post_tag' ) );
}
/**
* Retrieve the tags for a post formatted as a string.
*
* @since 2.3.0
*
* @param string $before Optional. Before tags.
* @param string $sep Optional. Between tags.
* @param string $after Optional. After tags.
* @param int $id Optional. Post ID. Defaults to the current post.
* @return string|false|WP_Error A list of tags on success, false if there are no terms, WP_Error on failure.
*/
function get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ) {
/**
* Filters the tags list for a given post.
*
* @since 2.3.0
*
* @param string $tag_list List of tags.
* @param string $before String to use before tags.
* @param string $sep String to use between the tags.
* @param string $after String to use after tags.
* @param int $id Post ID.
*/
return apply_filters( 'the_tags', get_the_term_list( $id, 'post_tag', $before, $sep, $after ), $before, $sep, $after, $id );
}
/**
* Retrieve the tags for a post.
*
* @since 2.3.0
*
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
*/
function the_tags( $before = null, $sep = ', ', $after = '' ) {
if ( null === $before )
$before = __('Tags: ');
$the_tags = get_the_tag_list( $before, $sep, $after );
if ( ! is_wp_error( $the_tags ) ) {
echo $the_tags;
}
}
/**
* Retrieve tag description.
*
* @since 2.8.0
*
* @param int $tag Optional. Tag ID. Will use global tag ID by default.
* @return string Tag description, available.
*/
function tag_description( $tag = 0 ) {
return term_description( $tag );
}
/**
* Retrieve term description.
*
* @since 2.8.0
* @since 4.9.2 The `$taxonomy` parameter was deprecated.
*
* @param int $term Optional. Term ID. Will use global term ID by default.
* @param null $deprecated Deprecated argument.
* @return string Term description, available.
*/
function term_description( $term = 0, $deprecated = null ) {
if ( ! $term && ( is_tax() || is_tag() || is_category() ) ) {
$term = get_queried_object();
if ( $term ) {
$term = $term->term_id;
}
}
$description = get_term_field( 'description', $term );
return is_wp_error( $description ) ? '' : $description;
}
/**
* Retrieve the terms of the taxonomy that are attached to the post.
*
* @since 2.5.0
*
* @param int|object $post Post ID or object.
* @param string $taxonomy Taxonomy name.
* @return array|false|WP_Error Array of WP_Term objects on success, false if there are no terms
* or the post does not exist, WP_Error on failure.
*/
function get_the_terms( $post, $taxonomy ) {
if ( ! $post = get_post( $post ) )
return false;
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
if ( ! is_wp_error( $terms ) ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
}
}
/**
* Filters the list of terms attached to the given post.
*
* @since 3.1.0
*
* @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
* @param int $post_id Post ID.
* @param string $taxonomy Name of the taxonomy.
*/
$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );
if ( empty( $terms ) )
return false;
return $terms;
}
/**
* Retrieve a post's terms as a list with specified format.
*
* @since 2.5.0
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @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( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $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[] = '' . $term->name . '';
}
/**
* Filters the term links for a given taxonomy.
*
* The dynamic portion of the filter name, `$taxonomy`, refers
* to the taxonomy slug.
*
* @since 2.5.0
*
* @param array $links An array of term links.
*/
$term_links = apply_filters( "term_links-{$taxonomy}", $links );
return $before . join( $sep, $term_links ) . $after;
}
/**
* Retrieve 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;
}
/**
* Display the terms in a list.
*
* @since 2.5.0
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return false|void False on WordPress error.
*/
function the_terms( $id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
$term_list = get_the_term_list( $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 array $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 );
}
/**
* Check if the current post has any of given category.
*
* @since 3.1.0
*
* @param string|int|array $category Optional. The category name/term_id/slug or array of them to check for.
* @param int|object $post Optional. Post to check instead of the current post.
* @return bool True if the current post has any of the given categories (or any category, if no category specified).
*/
function has_category( $category = '', $post = null ) {
return has_term( $category, 'category', $post );
}
/**
* Check 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.
*
* 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)
* Prior to v2.7, this function could only be used in the WordPress Loop.
* As of 2.7, the function can be used anywhere if it is provided a post ID or post object.
*
* @since 2.6.0
*
* @param string|int|array $tag Optional. The tag name/term_id/slug or array of them to check for.
* @param int|object $post Optional. Post to check instead of the current post. (since 2.7.0)
* @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
*/
function has_tag( $tag = '', $post = null ) {
return has_term( $tag, 'post_tag', $post );
}
/**
* Check 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 array of them to check for.
* @param string $taxonomy Taxonomy name
* @param int|object $post Optional. Post to check instead of the current post.
* @return bool True if the current post has any of the given tags (or any tag, if no tag specified).
*/
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;
}