需要帮助使用get_the_tag_list($ ID)WordPress

时间:2021-11-14 02:01:45

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

我正在制作一个新的WordPress模板,我想以文本格式获得与帖子相关的标签列表。我在用

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 ?

但问题是它返回URL和文本。是否有任何方法可以将标签的“文本”附加到由逗号分隔的帖子中?

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

即tag1,tag2,tag3,tag4等没有URL和文本?

Thanks

2 个解决方案

#1


2  

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.

模板标记get_the_tags()返回与Loop中当前在上下文中的帖子关联的所有标记的数组。您可以遍历此数组并手动生成逗号分隔列表。

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

以下是使用implode和print_r函数如何执行此操作的示例:

<?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


1  

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

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

#1


2  

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.

模板标记get_the_tags()返回与Loop中当前在上下文中的帖子关联的所有标记的数组。您可以遍历此数组并手动生成逗号分隔列表。

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

以下是使用implode和print_r函数如何执行此操作的示例:

<?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


1  

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

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