php - WordPress 回显数组

标签 php wordpress

我有以下代码行:$terms = get_the_terms( $the_post->ID, 'posts_tags' ); echo $terms; 这个想法是以标签的格式回显数组,但它只返回单词 Array?

我该怎么做?标签应该像这样呈现:'tag1, tag2, tag3'

最佳答案

尝试

foreach($terms as $term){
    echo $term;
}

你可能想添加一些东西来分隔它们,比如 $echo "," 或其他东西,但你明白了
你也可以用这个

$termsString = implode (',' , $terms);
echo $termsString;

根据要求:

术语确实是一个数组,例如查看@ProdigySim 的答案以了解它的外观。您确实可以使用 var_dumpprint_r 出于调试目的打印它们,但这在生产环境中对您没有帮助。

假设它不是一个关联数组,你可以这样找到第一个标签:

echo $terms[0]

其他的都是

echo $terms[1]

一直到最后一个 (count($terms)-1)。
foreach 按顺序打印每一个。第二个,您可以在 the manual 中找到只做一串。

最后,正如您在评论中所要求的那样:只需粘贴即可。

$terms = get_the_terms( $the_post->ID, 'posts_tags' ); 
$termsString = implode (',' , $terms);
echo $termsString;

关于php - WordPress 回显数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5358001/

相关文章:

html - 如何使用 css,html 在 wordpress 的图像(链接)上叠加文本

php - 使用 PHP 创建大型 xml 文件的最佳方法是什么?

php - AJAX:显示不同的 SQL 查询

javascript - FormData 添加文件不起作用

php - 安装 Laravel 5.5

jquery - wordpress 上的 $(window).height() 数量太大

php - 为什么我必须反序列化这个序列化值两次? (wordpress/bbpress Maybe_serialize)

php - Laravel 5.4 Cashier - 弥合文档方面的差距

php - 如何在 wordpress 导航中不影响子项的情况下突出显示处于事件状态的父项?

.net - ASP.NET的BlogEngine.Net是否可以叠加到Wordpress?