返回介绍

get_the_tags()

发布于 2017-09-11 00:38:14 字数 4886 浏览 1457 评论 0 收藏 0

get_the_tags( int $id )

Retrieve the tags for a post.


description


参数

$id

(int) (Required) Post ID.


返回值

(array|false|WP_Error) Array of tag objects on success, false on failure.


源代码

File: wp-includes/category-template.php

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' ) );
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/category-template.php: get_the_terms()
  • wp-includes/category-template.php: get_the_tags
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/feed.php: get_the_category_rss()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by hearvox

    This example prints the tags of current post:
    Code must be used in The Loop.

    
    $post_tags = get_the_tags();
    
    if ( $post_tags ) {
    	foreach( $post_tags as $tag ) {
        echo $tag->name . ', '; 
    	}
    }
    
  2. Example using post ID to get tags:
    This doesn’t need to be in The Loop.

    
    $post_tags = get_the_tags( 24 );
    
    print_r( $post_tags );
    
    /*
    This above prints the tag objects for post ID

    Print only the first tag name:

    
    $post_tags = get_the_tags();
    if ( $post_tags ) {
        echo $post_tags[0]->name; 
    }
    

    Execute code based on different tag values:
    This code displays different HTML for different tags. Add elseif statements as needed.

    
    <?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;
    ?>
    

    Function to show tags in a dropdown:

    
    <?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>
    

    Loop with tag post by ID and Link to -> /tag/slug:

    
    
                                  // 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="<?php bloginfo('url');?>/tag/<?php print_r($tag->slug);?>">
                                                    <?php print_r($tag->name); ?>
                                           </a>	
                                        </li>
                                        <?php endforeach; ?>
          </ul>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文