在由帖子组成的页面上显示自定义分类术语


Displaying Custom taxonomy term on page made up of posts

我正在尝试使用为该页面上的每个帖子显示一个分类术语

<?php foreach(get_the_terms($wp_query->post->ID, 'stock') as $term) echo $term->slug; ?>

我以前在body类中使用过这个,所以我可以更好地设计页面样式,但我不确定为什么它在这里不起作用。。。

有人能帮忙吗?

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <?php $catquery = new WP_Query( 'cat=10&posts_per_page=10' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="headingOne">
    <div class="container">
    <a data-toggle="collapse" class="collapsed" data-parent="#accordion" href="#collapse<?php echo $i; ?>" aria-expanded="true" aria-controls="collapseOne">
      <h4 class="panel-title"> 
        <?php the_title(); ?><div class="stock-level"> <?php foreach(get_the_terms($wp_query->post->ID, 'stock') as $term) echo $term->slug; ?></div>
        </h4></a> 
     </div>
    </div>
    <div id="collapse<?php echo $i; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
      <div class="panel-body">
      <div class="container">
      <div class="row">
      <div class="col-xs-12 col-sm-7 col-md-7 col-lg-7 recycled-image">
        <?php echo the_post_thumbnail(); ?>
      </div>
      <div class="col-xs-12 col-sm-5 col-md-5 col-lg-5">
       <h1><?php the_title(); ?></h1>
      <p><?php the_content(); ?></p>
      </div>
      </div>
      </div>
      </div>
    </div>
     </div>
    <?php $i++; endwhile; ?>

如果这更像是一个存档页面,而不是使用$wp_query->post->ID的单个页面,则不会返回存档中所有帖子的ID。

你必须做一些类似的事情:

$args = array('YOUR POST ARGS');
$posts = get_posts($args);
foreach ($posts as $post ) {
    setup_postdata( $post );
    foreach(get_the_terms(get_the_ID(), 'stock') as $term){
        echo $term->slug.'<br />';
    }
}