Varsity
Member
Posted 3 years ago #
How would I go about this? I've tried using get_the_tags(), but while I can set up the PHP correctly to cycle three times, it seems I can't actually access the data...
$tags = get_the_tags(); if ($tags) { for ($i=0;$i<=2;$i+=1) { echo $tags[$i]->name . ' '; } }
From the Codex example at get_the_tags
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ' ';
}
}
?>
Or look at get_the_tag_list.
Varsity
Member
Posted 3 years ago #
I'm pretty sure that both of those suggestions will display all of the tags.
Sorry missed the goal ;)
<?php
$count = 0;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if ($count <= 3 ) {
echo $tag->name . ' ';
}
}
}
?>
Is it possible to do this but also make the tags links to the tag archive?
Varsity
Member
Posted 3 years ago #
Where would you use that? Inserting it in place of get_the_tags() breaks the code.
Varsity
Member
Posted 3 years ago #
This should do the job:
<?php
$count = 0;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$count++;
if ($count <= 3 ) {
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a> ';
}
}
}
?>