Pergunta

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.

Foi útil?

Solução

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

Outras dicas

$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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top