WordPress 回显数组 [英] WordPress echo out array
问题描述
我有以下代码行: $terms = get_the_terms( $the_post->ID, 'posts_tags' );echo $terms;
这个想法是以标签的格式回显数组,而不是它只返回单词Array?
I have the following line of code: $terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms;
The idea is to echo out the array in the format of the tags but instead it just returns the word Array?
我该怎么做?标签应如下所示:'tag1, tag2, tag3'
How do I do this? The tags should be presented like this: 'tag1, tag2, tag3'
推荐答案
try
foreach($terms as $term){
echo $term;
}
您可能想添加一些东西来分隔它们,例如 $echo ","
或其他东西,但您明白了
你也可以用这个
you might want to add something to separate them, like a $echo ","
or something, but you get the idea
You can also use this
$termsString = implode (',' , $terms);
echo $termsString;
根据要求:
条款确实是一个数组,例如,请参阅@ProdigySim 对其外观的回答.您确实可以使用 var_dump
或 print_r
出于调试目的打印它们,但这在生产环境中对您没有帮助.
The terms is indeed an array, see for instance @ProdigySim 's answer for how it looks. You could indeed print them for debuggin purposes with var_dump
or print_r
, but that would not help you in a production environment.
假设它不是一个关联数组,你可以像这样找到第一个标签:
Assuming that it is not an associative array, you could find the first tag like this:
echo $terms[0]
其他都喜欢
echo $terms[1]
一直到最后一个 (count($terms)-1
).
foreach 按顺序打印每一个.第二个,正如您在 手册 中找到的那样,只制作其中的一个字符串.
Right up until the last one (count($terms)-1
).
The foreach prints each of these in order. The second, as you can find in the manual just makes one string of them.
最后,正如您在评论中所问的那样:只需粘贴即可.
In the end, as you asked in the comments: Just paste this.
$terms = get_the_terms( $the_post->ID, 'posts_tags' );
$termsString = implode (',' , $terms);
echo $termsString;
这篇关于WordPress 回显数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!