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
1 Answer
Reset to default 0Your 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
评论列表(0条)