How do I list custom taxonomy terms without the links?

The question:

I’ve searched far and wide to try and find an answer to my question. I’m hoping I can get help here. Here goes…

I’m currently retrieving the terms of my custom taxonomy using:

<?php echo get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ); ?>

What I’m trying to do is retrieve these same post-specific custom taxonomy terms in a list without them being output as links. I’ve tried all of the following “solutions,” but none of them work. Any help would be appreciated.

Returns the post-specific terms in one long string that can’t be put in a list:

$terms_as_text = get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ) ;
echo strip_tags($terms_as_text);

Returns a list of all the terms used across all the custom post types:

<ul>
<?php $args = array( 'taxonomy' => 'skills', 'orderby' => 'ID', 'order' => 'ASC' );
$categories = get_categories( $args );
foreach($categories as $category) { echo '<li> '. $category->name . '</li>'; } 
                ?>
</ul>

Returns nothing:

<?php $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
wp_get_object_terms( $post->ID, $skills, $args );
?>

I’ve even tried get_the_terms, get_terms, and get_categories to no avail.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Can try this:

$terms = get_the_terms ($post->id, 'skills');
if ( !is_wp_error($terms)) : ?>

<?php 
    $skills_links = wp_list_pluck($terms, 'name'); 

    $skills_yo = implode(", ", $skills_links);
    ?>

    <span><?php echo $skills_yo; ?></span>

Method 2

Just use strip_tags

<?php echo strip_tags(get_the_term_list( $post->ID, 'CUSTOM-TAXONOMY', ' ',', ')); ?>

Method 3

$terms = wp_get_post_terms($post->ID, 'TAXONOMYNAME');
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}

Method 4

function term_clean($postid, $term)
{
    $terms = get_the_terms($postid, $term); 
    foreach ($terms as $term) {  echo $term->name;   };

}

Method 5

If you just want the terms assigned to a specific post:

<?php $object_terms = wp_get_object_terms( $post_id, 'skills', array( 'fields' => 'names' ) );
if ( $object_terms ) { ?><ul><li><?php echo implode( '</li><li>', $object_terms ); ?></li></ul><?php } ?>

If you want ALL of the terms:

<?php $all_terms = get_terms( 'skills', array( 'fields' => 'names' ) );
if ( $all_terms ) { ?><ul><li><?php echo implode( '</li><li>', $all_terms ); ?></li></ul><?php } ?>

Method 6

I ran into a similar problem yesterday, and came up with the follow solution:

function taxonomy_list( $taxonomy ) {
    $args = array('order'=>'ASC','hide_empty'=>false);
    $terms = get_terms( $taxonomy, $args );
    if ( $terms ) {
        printf( '<ul name="%s">', esc_attr( $taxonomy ) );
        foreach ( $terms as $term ) {
            printf( '<li>%s</li>', esc_html( $term->name ) );
        }
        print( '</ul>' );
    }
}

Then, just paste <?php taxonomy_list( 'TAXONOMY ID' ); ?> in your template file, replacing TAXONOMY ID with whatever the name of the taxonomy is.

My original usage was to create a list of the job categories I have on my job board. Each one then linked to the taxonomy’s archive. You can see the full function in my answer on my own Stackoverflow question.

Method 7

// to display taxonomy terms without links: separated with commas
// copy this code in your function.php

function get_taxonony_toDisplay($post_id, $taxonomy_name) {
$terms = wp_get_post_terms($post_id, $taxonomy_name);
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
}

Since I had to display 3 taxonomies separated with commas, so I made a function using Henry’s code.

To display use the following line:

<?php get_taxonony_toDisplay($post->ID, 'your_taxonomy_name' ); ?> 

Method 8

If you want the terms ordered by slug rather than name then use this…

<?php if(has_term('', 'CUSTOM-TAX')) {?> 
<?php
    $custom_terms = get_the_terms( get_the_ID(), 'CUSTOM-TAX' );
    // Make sure we have terms and also check for WP_Error object
    if (    $product_terms
    && !is_wp_error( $product_terms )
    ) {
    @usort( $product_terms, function ( $a, $b )
    {
    return strcasecmp( 
    $a->slug,
    $b->slug
    );
    });
    // Display your terms as normal
    $term_list = [];
    foreach ( $custom_terms as $term ) 
    //$term_list[] = esc_html( $term->name );  // comment this line out if you want the terms linked  and visa versa
    $term_list[] = '<a href="' . get_term_link( $term ) . '" rel="nofollow noreferrer noopener">' . esc_html( $term->name ) . '</a>'; // comment this line out if you DON'T want the terms linked and visa versa
    echo implode( ', ', $term_list );
    }
?>                          
<?php } else { ?><?php }?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment