Question

In Wordpress I created a taxonomy called "Cities" for a CPT called "People". I want to display all taxonomy tags selected for a person in the template of the single.php-file.

What is the code for displaying only those elements selected?

I use the following code in the single-people.php:

<?php
$taxonomy = 'cities';
$terms = get_terms($taxonomy); 

if ( $terms && !is_wp_error( $terms ) ) :
?>
  <ul>
  <?php foreach ( $terms as $term ) { ?> 
    <li><a href="<?php echo wp_get_post_terms($term -> slug, $taxonomy); ?>"><?php echo $term -> name; ?></a></li>
  <?php 
  } ?>
  </ul>
<?php endif; ?>     

I want only the selected taxonomy tags without link.

Was it helpful?

Solution

I understand you want to display all the terms of a taxonomy selected for a single post.

You need to use the function wp_get_post_terms() like this:

$taxonomy = 'cities';
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );

foreach ( $post_terms as $term ) {
    echo $term; 
}

This will print all the names of the cities the current post is related to. Now you have to adapt this code to your page and, if you don't want links, just do not use <a> tags...

OTHER TIPS

$wrap = 'hello';
$terms = wp_get_post_terms(get_the_ID( ), $wrap, array("fields"=>"names"));
foreach ( $terms as $term ) {
  echo $term;
}

in the place of hello you take the taxonomy name and it surely run

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top