0
votes
function hashtags(){
    $tags = get_the_tags($post->ID);
    $count=0;
    foreach ($tags as $tag){
    $count++;
    if (1 == $count) {
    return $tag->name . ', '; 
    }
    if (2 == $count) {
    return $tag->name . ', '; 
    }
    if (3 == $count) {
    return $tag->name; 
    }
    }
}

I don't know about php, i'm noob, i made this function for showing the name of the first 3 tags of post, i want this return: tag1, tag2, tag3.

The function works but only return the first tag, if i put echo no problem but i don't want an echo, any idea?

1

1 Answers

1
votes

Sorry if I've misunderstood but I think you're trying to return a comma separated list of the names found by the get_the_tags function? If so this should work:

$tags = get_the_tags($post->ID);
$names = array();
$count = 1;
foreach ($tags as $tag) {
  $names[] = $tag->name;
  if ($count++ == 3) {
    break;
  }
}

return implode(', ', $names);

That code loops through the tags, adds each tag name to an array ($names), and finally runs the array through implode() to generate the comma separated list.