Listing added Taxonomies to CPT [Solved]

I have a CPT with a Taxonomy setup. I want to list the tags on the post that are attached to the CPT.

This is what I am using now in the CPT PHP. What am I doing wrong?

<?php $terms = get_the_terms( get_the_ID(), 'properties' ); if ( $terms && ! is_wp_error( $terms ) ){ foreach ( $terms as $term ) { echo $term->name; } } ?>

I want to pull both taxonomies (property_types and amenities) because they’re linked to the properties CPT.

Thanks.

Hi there,

Thanks for posting in.

It would best to contact the plugin developer to see what’s going on and have it fixed it for you. But, maybe I can help you direct to the issue, I think the issue is this line.

$terms = get_the_terms( get_the_ID(), 'properties' );

Both property_types and amenities are taxonomies, but properties isn’t. If you’ll check the doc here https://developer.wordpress.org/reference/functions/get_the_terms/. That function accepts taxonomy so it should be like

$terms = get_the_terms( get_the_ID(), 'property_types' );

Thanks!

I tried your code, it didn’t work. Is there something wrong with my taxonomy setup possibly? I’m just trying to display the taxonomies on each single post view of the cpt and then I can worry about styling at later.

So this code i posted earlier is in the code loop for the single post to display, with your adjusted line.

But here’s my setup, maybe I did it incorrectly.

Hello There,

Could you please post your entire PHP code in your next reply?

Thanks.

Yes, I solved it with this:

<div class="wtf">
<?php $term_obj_list = get_the_terms( $post->ID, 'property_type' );
$terms_string = join(', ', wp_list_pluck($term_obj_list, 'name'));?>
<div class="property_type">
<p>Property Type: <?php echo $terms_string;?></p>
<?php $term_obj_list = get_the_terms( $post->ID, 'amentity' );
$terms_string = join(', ', wp_list_pluck($term_obj_list, 'name'));?>
<div class="amentity">
<p>Amentities: <?php echo $terms_string;?></p>
</div>

Thank you!

Great! We are just glad that you have figured it out a way to correct the said issue.
Thanks for letting us know!

Best Regards.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.