views:

386

answers:

3

Hi Guys,

I am making a new wordpress template and I want to just get, in text format, the list of tags associated with a post. I am using

get_the_tag_list($id)

But the problem is that it returns the URL as well as the text. Is there any way to just get the "text" of tags attached to a post seperated by a comma ?

i.e. "tag1, tag2, tag3, tag4 etc" without the URL and just as text?

Thanks

+2  A: 

The template tag get_the_tags() returns an array of all of the tags associated with the post currently in-context within the Loop. You could traverse this array and generate a comma-separated list by hand.

Here's an example of how you could do it using the implode and print_r functions:

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach ($posttags as $tag) {
     $tagnames[count($tagnames)] = $tag->name;
  }
  $comma_separated_tagnames = implode(", ", $tagnames);
  print_r($comma_separated_tagnames);
}
?>
Evan Meagher
thanks for the response - any ideas how to do this :)?
hey - this is perfect but it returns ALL the tags - not just the tags for a post ?
any ideas how to get it to return tags JUST for a single post ?
The code above *will* return the tags for a single post if you put it in the Loop for a single post. You don't send get_the_tags an ID, so it's context sensitive. If you're using this code in a more global context, you could test against $tag->term_id within the foreach to filter out the tags you're not interested in.
Evan Meagher
thanks again for the response - the problem i am facing when i use this code is that tags are "aggregated"i.e. it prints tags at the top post fine - only the top post tags appear, but then in the 2nd post - it prints the top post tags and the second post tags and so on. third post, 1st, 2nd and 3rd post tags etc etchow to restrict tags in this code, to just show for one post ?
i.e. this code works perfect<?php$posttags = get_the_tags();if ($posttags) {foreach($posttags as $tag) {echo $tag->name . ' '; }}?>and it returns only the tags for each post. yours work great - since i can print $comma_separate_tagnames in echo - but I'm not a PHP coder so not sure how to change the code to not make to "count up / aggregate" the tags - and instead just show tags per individual post
any ideas about this ?
add $tagnames = array(); before the foreach loop.
hayato
A: 

Same question here: http://wordpress.org/support/topic/271945?replies=2

Michael
A: 
<?php
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
echo $tag->name . ','; 
}
}
?>

Source: http://codex.wordpress.org/Template_Tags/get_the_tags

mr-euro
I missed that it is nearly identical to Evan's reply. It should still work as requested inside the Loop.On my own blog I use the_tags( ' ', ', ', ''); to print a list of tags out after the post title. Those are with links inclusive though so I guess it is not useful for you.
mr-euro