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

Php 在由帖子组成的页面上显示自定义分类术语,php,wordpress,taxonomy,Php,Wordpress,Taxonomy,我试图使用 <?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 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\u 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 />';
    }
}