php - 如何输出用于 WP 帖子的分类术语?

标签 php html wordpress taxonomy custom-taxonomy

我目前正在 wordpress 中开展一个项目,在该项目中,我为应用于该个别帖子的服务列表创建了自定义分类法。

<main id="main" role="main" <?php body_class(); ?>>
<?php
     $services_args = array(
        'post_type' => 'sc_project',
     );
     $services_query = new WP_Query( $services_args );
     if ( $services_query->have_posts() ):

     $terms = get_terms( array(
         'taxonomy' => 'sc_project_service',
         'hide_empty' => false,
     ) );
?>  
<section>
  <div>
       <a href="#">Back to Projects</a>
       <h2><strong><?php the_title(); ?></strong></h2>
       <div>
           <div>Services</div>
           <?php foreach ( $terms as $term ) :  ?>
              <span><?php echo $term->name; ?></span>
           <?php endforeach; ?> 
      </div>
  </div>
</section>
<?php endif; ?>
</main>

所以我的问题是,通过这段代码,我能够输出每一个使用的分类术语,但我该如何将输出限制为仅针对该特定帖子的输出?

如有任何意见,我们将不胜感激。

谢谢!

最佳答案

使用wp_get_post_terms()而不是 get_terms():

$terms = wp_get_post_terms( get_the_ID(), 'sc_project_service' );

关于php - 如何输出用于 WP 帖子的分类术语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40307935/

相关文章:

java - 参数拦截器错误: Unexpected Exception caught,设置表达式错误

html - FF 上的位置问题,但 Chrome/IE8+ 上没有

jquery - 如何在 Jquery 数据表中将搜索条件导出到 excel/csv

html - 短代码无法正常工作

php - 在 Woocommerce 中将 Avada Catalog 排序 Hook 覆盖回默认值

php - MediaWiki 1.20.0 中如何通过大小写用户名搜索用户贡献?

php - IE7 中一个子文件夹的 $_POST 变量为空

Wordpress - meta_query - 可以使用多个或嵌套的 meta_query 参数吗?

php - 将表单提交给 php 文件中的特定函数

javascript - 将 PHP 变量传递到 float 条形图中