我有一个名为“的自定义帖子类型”football_team
“这个自定义帖子有一个名为巴塞罗那的帖子,id为“post=7”single-football_team.php
将此帖子显示为的模板:
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'content-parts/content\', \'team\' ); ?>
<?php endwhile; ?>
在中
content-parts/content-team.php
文件我使用以下代码:
<?php
$terms = get_terms( \'competition\', array(
\'orderby\' => \'count\',
\'hide_empty\' => true,
) );
?>
<?php
foreach( $terms as $term ) {?>
<section class="achievement-section clearfix">
<?php $counter = 0; ?>
<div class=" league-and-season clearfix">
<div class="honour-league-name"> <?php echo $term->name; ?> </div>
<?php
$team = get_post_meta( get_the_ID(), \'football_team_team_name\', true );
$args = array(
\'post_type\' => \'football_league\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'competition\',
\'field\' => \'slug\',
\'terms\' => $term
),
),
\'meta_query\' => array(
array(
\'key\' => \'football_league_team_name\',
\'value\' => $team,
\'compare\' => \'=\'
),
),
);
$fixture_query = null;
$fixture_query = new WP_Query($args); ?>
<div class="honour-season-name">
<ul >
<?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
<?php
$champion_team = get_post_meta( get_the_ID(), \'football_league_team_name\', true );
$terms_competition = get_the_terms( get_the_ID(), \'competition\' );
$terms_season = get_the_terms( $post->ID , \'season\' ); ?>
<?php
foreach ($terms_season as $object) {?>
<li> <?php echo $object->name; ?></li>
<?php }
?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
</div>
</div>
<div class="achievement-times"><span><?php echo $counter; ?> </span></div>
</section>
<?php }
?>
在以上代码中,以下行是团队名称的元框
football_team
自定义帖子:
$team = get_post_meta( get_the_ID(), \'football_team_team_name\', true )
;
AND
<上述代码显示如下:
但问题是:
英超联赛
意大利甲级联赛
上述三个竞争分类学术语与第7号职位(巴塞罗那)无关。我认为它显示了由于get\\u term函数和foreach循环而产生的所有术语。有没有办法只提取与post=7或Barcelona相关的特定分类术语?任何解决方案都将不胜感激!
SO网友:attoma
我使用了以下代码,它工作得很好。
<?php
$terms = wp_get_post_terms($post->ID, \'competition\'); ?>
<?php
$count = count($terms);
if ( $count > 0 ) {
foreach( $terms as $term ) {?>
<section class="achievement-section clearfix">
<?php $counter = 0; ?>
<div class=" league-and-season clearfix">
<div class="honour-league-name"> <?php echo $term->name; ?> </div>
<?php
$team = get_post_meta( get_the_ID(), \'football_team_team_name\', true );
$args = array(
\'post_type\' => \'football_league\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'competition\',
\'field\' => \'slug\',
\'terms\' => $term
),
),
\'meta_query\' => array(
array(
\'key\' => \'football_league_team_name\',
\'value\' => $team,
\'compare\' => \'=\'
),
),
);
$fixture_query = null;
$fixture_query = new WP_Query($args); ?>
<div class="honour-season-name">
<ul >
<?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
<?php
$champion_team = get_post_meta( get_the_ID(), \'football_league_team_name\', true );
$terms_competition = get_the_terms( get_the_ID(), \'competition\' );
$terms_season = get_the_terms( $post->ID , \'season\' ); ?>
<?php
foreach ($terms_season as $object) {?>
<li> <?php echo $object->name; ?></li>
<?php }
?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
</div>
</div>
<div class="achievement-times"><span><?php echo $counter; ?> </span></div>
</section>
<?php }
}
?>