我正试图回应帖子上的一个标签。有多个标签,大约40个。
目前我正在尝试:
<?php
$tag = get_tag(16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58);
echo $tag->name;
?>
编辑:
function misha_filter_function(){
$args = array(
\'orderby\' => \'date\', // we will sort posts by date
\'order\' => $_POST[\'date\'] // ASC или DESC
);
$args = array(
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'post_tag\',
\'field\' => $cat_id,
\'terms\' => $_POST[\'locationfilter\'],
),
)
);
$relation = \'AND\';
if( isset( $_POST[\'timefilter\'] ) )
$args[\'tax_query\'] = array(
\'relation\' => $relation,
array(
\'taxonomy\' => \'post_tag\',
\'field\' => $tag_id,
\'terms\' => $_POST[\'timefilter\']
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post(); ?>
<!-- post -->
<a href="<?php the_permalink()?>">
<div class="col-md-3 col-sm-6 ver-item">
<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), \'full\' );?>
<div class="thumb" style="background-image:url(\'<?php echo $thumb[\'0\'];?>\');"></div>
<section>
<time>
<!-- here is where the tag should be displayed -->
<?php $post_tags = get_the_tags(\'YOUR POST ID\');
if ( $post_tags ) {
echo $post_tags[0]->name;
}?>
</time>
<h3><?php the_title();?></h3>
<span><!-- underline --></span>
</section>
</div>
</a>
<?php endwhile;
wp_reset_postdata(); else :
echo \'Geen resultaten, probeer het opnieuw.\';
endif;
die();
但问题是,它只在所有帖子上输出第一个标签。这是id为16的标签。它甚至将标签id 16回传给其他帖子,而这些帖子甚至没有该标签。
如果帖子有该标签/有其中一个标签ID,我如何只能回显其中一个标签?
最合适的回答,由SO网友:anton 整理而成
作用get_tag
按标记ID或标记对象检索post标记
此标签对象未与当前帖子连接
使用get_the_tags
检索循环中当前帖子的标记
$post_tags = get_the_tags();
if ( $post_tags ) {
echo $post_tags[0]->name;
}
将帖子id添加到
get_the_tags
函数检索此帖子的标签,无需循环
$post_tags = get_the_tags(\'YOUR POST ID\');
if ( $post_tags ) {
echo $post_tags[0]->name;
}
使用检查包含的标记数组中是否存在类别
in_array
//outside the loop
$date_tags = array(16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58);
//inside the loop
//shows only first tag name if it finds one or more
$post_tags = get_the_tags();
if ( $post_tags && in_array($post_tags[0]->term_id, $date_tags ) ) {
echo $post_tags[0]->name;
}