2
<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ' ', '' ); ?> 

returns something like this:

People: <a href="person1">Person1</a>, <a href="person2">Person2</a>, ...

How can I make it return the same thing without links like this:

People: Person1, Person2

4 Answers 4

12

It may be easier to just write the list manually, something like:

<?php
$terms = wp_get_post_tags( $post->ID );
//For custom taxonomy use this line below
//$terms = wp_get_object_terms( $post->ID, 'people' );

foreach( $terms as $term )
    $term_names[] = $term->name;

echo implode( ', ', $term_names );
2
  • How would this work for a custom taxonomy? I tried: $terms = get_the_term_list( $post->ID,'people'); but that gave me an error
    – Carson
    Commented Apr 19, 2011 at 5:22
  • @Carson you are using the wrong function there, you want wp_get_object_terms() so it would be: $terms = wp_get_object_terms( $post->ID, 'people' );
    – Joe Hoyle
    Commented May 30, 2011 at 23:23
13

Alternatively you can also use

<?php 
echo strip_tags (
    get_the_term_list( get_the_ID(), 'tax_name', "Text Before Value ",", " )
);
?>
0
1

I found another method that more directly answers my question:

<?php $terms_as_text = get_the_term_list( $post->ID,'people', 'People: ', ', ');
if (!empty($terms_as_text)) echo '<p>', strip_tags($terms_as_text) ,'</p>'; ?>

Credit: CSS Tricks

1
1

Using strip_tags() Can get complicated if you want to show your terms as an HTML list. Here, there's something for you. $raw set to true (or anything that's not empty) will just create an inline list with the $separator of your choice, if not, it will generate an HTML list without links. If you want your list to have a styled title, set the $titletag to, say H1 or H2. If you don't want a title, just leave $title empty.

function show_tax($taxname, $title, $title_tag, $raw, $separator){
    $terms = get_the_terms($post->ID, $taxname);
    $out = '';
    if (!empty($title)){
        if(empty($title_tag)){
            $title_tag = 'span';
           }
            $out .= '<'.$title_tag.'>'.$title.'</'.$title_tag.'>';
        }
    if (!empty($raw)){
                $out = implode($separator, $terms);
        }
        else{

            $out .= '<ul>';
                foreach ( $terms as $term ){
                            $out .='<li>'.$term->name.'</li> ';
                            }
                $out .= '</ul>';

        }       
            return $out;
}

Example of use:

echo show_tax('people', 'PEOPLE', 'h3', '', ''); // An html list with PEOPLE as title
echo show_tax('people', 'PEOPLE:', '', true, ','); // An inline list with PEOPLE: as before text
1
  • updated code. implode didn't work cause it is an array of post objects
    – MikeiLL
    Commented Apr 21, 2017 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.