如何显示与特定职位相关联的分类术语列表?

时间:2017-05-04 作者:attoma

我有一个名为“的自定义帖子类型”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

<上述代码显示如下:Taxonomy但问题是:

英超联赛

意大利甲级联赛

上述三个竞争分类学术语与第7号职位(巴塞罗那)无关。我认为它显示了由于get\\u term函数和foreach循环而产生的所有术语。有没有办法只提取与post=7或Barcelona相关的特定分类术语?任何解决方案都将不胜感激!

1 个回复
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 }
}
?>

enter image description here

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的