^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_the_tags()用法示例

get_the_tags( int $id )

Retrieve the tags for a post.


描述


参数

$id

(int)
(Required)
Post ID.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


返回值

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

更新日志

Version 描述
2.3.0 Introduced.

相关函数

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()

  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>
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈