Get X post tags

How to properly get only e.g. 3 post tags? My current code:$post_tags = get_the_tags();if (!empty($post_tags)) {foreach

How to properly get only e.g. 3 post tags? My current code:

$post_tags = get_the_tags();
if (!empty($post_tags)) {
    foreach ($post_tags as $tag) {
        echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
    }
}

How to properly get only e.g. 3 post tags? My current code:

$post_tags = get_the_tags();
if (!empty($post_tags)) {
    foreach ($post_tags as $tag) {
        echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a>';
    }
}
Share Improve this question asked Sep 28, 2019 at 15:35 AvigoAvigo 377 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Your question seems similar to this one: how to limit and display tag?

Based on it you can use this code to limit the tags:

$post_tags = get_the_tags();
   shuffle($post_tags); // use this incase you want to pick the tags randomly
   $count = 0;
   if ($post_tags) {
       foreach($post_tags as $tag) {
          $count++;
          echo '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a> ';
          if( $count > 4 ) break;
       }
   }

It will return 3 tags only.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121762a4612454.html

相关推荐

  • Get X post tags

    How to properly get only e.g. 3 post tags? My current code:$post_tags = get_the_tags();if (!empty($post_tags)) {foreach

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信