need help in using get_the_tag_list($ID) WordPress

只谈情不闲聊 提交于 2019-12-02 03:25:50

问题


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


回答1:


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);
}
?>



回答2:


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

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



来源:https://stackoverflow.com/questions/880673/need-help-in-using-get-the-tag-listid-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!