我正在尝试与div中的数据类别标记中的帖子关联的标记的回显列表。目前我的代码在标签中没有输出任何内容。我也有输出“阵列”这个词的地方。我需要显示为'tag1 tag2 etc'
以下是我目前在if have posts loop ...
中设置代码的方法获得价值......
$post_tag = wp_get_post_tags( $post_id, $args );
回应价值......
<div data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>">
这是完整的代码......
<?php $layout = get_post_type(); ?>
<?
$slug = get_post( $post )->post_name;
$post_tag = wp_get_post_tags( $post_id, $args );
?>
<? if ( $layout == 'fullscreenimage' ){ ?>
<p>layout 1</p>
<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<?php foreach($post_tag as $tag){ echo $tag; } ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php } elseif ( $layout == 'singleimagetext' ) { ?>
<p>layout 2</p>
<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php } elseif ( $layout == 'casestudy' ) { ?>
<p>layout 3</p>
<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php } elseif ( $layout == 'gallery' ) { ?>
<p>layout 4</p>
<div class="portfolio-item" data-id="<?php echo $slug; ?>" data-category="<? //echo $tags; ?>">
<h1 class="entry-title"><?php the_title(); ?></h1>
</div>
<?php } ?>
<?php endwhile; ?>
<?php else : ?>
<h2 class=”center”>Not Found</h2>
<p class=”center”>Sorry, but you are looking for something that isn’t here.</p>
<?php endif; ?>
<?php /* end my loop */ ?>
答案 0 :(得分:0)
$ tag是std Class的一个对象,根据文档,为了得到标签名称,试试这个
<div data-category="<?php foreach($post_tag as $tag){ echo $tag->name; } ?>">
$ tag-&gt; name只是获取名为name的stdClass Object属性。
这是对象返回的内容,因此您可以获取URL的term_id或slug等其他信息。
stdClass Object
(
[term_id] => 4
[name] => tag2
[slug] => tag2
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => post_tag
[description] =>
[parent] => 0
[count] => 7
)
答案 1 :(得分:0)
为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(); ?>">