get_the_tags()
最后更新于:2021-11-27 02:43:28
get_the_tags( int|GC_Post$post_id)Retrieves the tags for a post.
参数
- $post_id
-
(int|GC_Post) (Required) Post ID or object.
响应
(GC_Term[]|false|GC_Error) Array of GC_Term objects on success, false if there are no terms or the post does not exist, GC_Error on failure.
源文件
文件: gc-includes/category-template.php
function get_the_tags( $post_id = 0 ) {
$terms = get_the_terms( $post_id, 'post_tag' );
/**
* Filters the array of tags for the given post.
*
* @since 2.3.0
*
* @see get_the_terms()
*
* @param GC_Term[]|false|GC_Error $terms Array of GC_Term objects on success, false if there are no terms
* or the post does not exist, GC_Error on failure.
*/
return apply_filters( 'get_the_tags', $terms );
}
$post_tags = get_the_tags( 24 ); print_r( $post_tags ); /* This above prints the tag objects for post ID #24 (if post has any tags): Array ( [0] => GC_Term Object ( [term_id] => 108 [name] => tag-1 [slug] => tag-1 [term_group] => 0 [term_taxonomy_id] => 109 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [filter] => raw [object_id] => 24 ) [1] => GC_Term Object ( [term_id] => 109 [name] => tag-2 [slug] => tag-2 [term_group] => 0 [term_taxonomy_id] => 110 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [filter] => raw [object_id] => 24 ) ) */
<?php $post_tags = get_the_tags(); foreach( $post_tags as $tag) : if ( $tag->name === 'tag-1' ) : ?> <p>Display HTML for posts tagged with 'tag-1'.</p> <?php elseif ( $tag->name === 'tag-2' ) : ?> <p>Display HTML for posts tagged with 'tag-2'.</p> <?php else : // Post has neither tag, do nothing. endif; endforeach; ?>
<?php function dropdown_tags(){ echo '<select name="tag" id="tag" class="postform">'; foreach ( get_the_tags() as $tag ) { echo '<option value="' . $tag->name . '">' . $tag->name . "</option>n"; } echo '</select>'; } ?> <h2><?php _e( 'Tags:', 'textdomain' ); ?></h2> <form id="tags-select" class="tags-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get"> <?php dropdown_tags(); ?> <input type="submit" name="submit" value="view" /> </form>
// GET TAGS BY POST_ID $tags = get_the_tags($post->ID); ?> <ul class="cloudTags"> <?php foreach($tags as $tag) : ?> <li> <a class="btn btn-warning" href="https://docs.gechiui.com/functions/get_the_tags/<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>"> <?php print_r($tag->name); ?> </a> </li> <?php endforeach; ?> </ul>