Buckk Buckk - 1 month ago 15
PHP Question

How to output taxonomy terms used for WP post?

I'm currently working on a project in wordpress where I have a custom taxonomy created for a list of services applied to that individual post.

<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>


So my question is, with this block of code I have the ability to output every single taxonomy term used but how do I go about limiting the output to only the ones for that specific post?

Any input would be appreciated.

Thank you!

Answer

Use wp_get_post_terms() instead of get_terms():

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