Php 回显与数据类别标记中的帖子关联的标记列表
我试图在div中的数据类别标记中回显与post相关联的标记列表。目前,我的代码在标记中没有输出任何内容。我还把它放在输出单词“Array”的地方。我需要显示为“tag1 tag2等” 下面是我目前如何在if have posts循环中设置代码的 获取值Php 回显与数据类别标记中的帖子关联的标记列表,php,wordpress,tags,Php,Wordpress,Tags,我试图在div中的数据类别标记中回显与post相关联的标记列表。目前,我的代码在标记中没有输出任何内容。我还把它放在输出单词“Array”的地方。我需要显示为“tag1 tag2等” 下面是我目前如何在if have posts循环中设置代码的 获取值 $post_tag = wp_get_post_tags( $post_id, $args ); <div data-category="<?php foreach($post_tag as $tag){ e
$post_tag = wp_get_post_tags( $post_id, $args );
<div data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>">
与价值观相呼应
$post_tag = wp_get_post_tags( $post_id, $args );
<div data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>">
根据文档,
$tag是std类的对象,要获得标记名,请尝试以下操作
<div data-category="<?php foreach($post_tag as $tag){ echo $tag->name; } ?>">
文档在functions.php中添加了一个函数
function my_post_terms() {
// Get an array of all taxonomies for this post
$taxonomies = get_taxonomies( '', 'names' );
// Are there any taxonomies to get terms from?
if ( $taxonomies ) {
// Call the wp_get_post_terms function to retrieve all terms. It accepts an array of taxonomies as argument.
$arr_terms = wp_get_post_terms( get_the_ID(), array_values( $taxonomies ) , array( "fields" => "names" ) );
// Convert the terms array to a string
$terms = implode( ' ',$arr_terms );
// Get out of here
return $terms;
}
}
然后添加了这个echo my_post_terms();在html中
<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<?php echo my_post_terms(); ?>">
谢谢,但这没有返回任何内容。我想得到的标签是自定义分类法,所以我不知道是否需要特殊处理?它只是显示..'“数据类别”这就是你的意思吗?啊,不,不是。。。如何进行变量转储?如果您有帖子的标签,上面的代码应该可以工作,我不知道这里的问题是什么。放置var_转储($post_标签);$post_tag=wp_get_post_tags($post_id,$args)之后;然后访问页面,看看您得到了什么输出?很抱歉响应延迟,但是var dump的输出是数组(0){}