我目前正在 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/