Question

I need to display the name of a custom taxonomy term in a page template, and I've tried with the following line:

<h2><?php the_terms( 0, 'my_category'); ?></h2>

This correctly retrieves the taxonomy and displays the desired term, but it also adds a link to that term archive page.

I'd like to display only a simple text, with no link at all.

How should I modify this?


To give more context:

<?php 

$args = array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
         array(
            'taxonomy' => 'my_category',
            'field'    => 'slug',
            'terms'    => 'my_term',
         ),
    ),
);

$my_query = new WP_Query( $args );

?> 

<div>

    <h2><?php the_terms( 0, 'my_category'); ?></h2>

</div>

<?php

while ( $my_query->have_posts() ) :
   $my_query->the_post(); 

?>

<div>

    <h3><?php the_title(); ?></h3>

    <div><?php the_content(); ?></div>

</div>


<?php

endwhile; 
wp_reset_query();
wp_reset_postdata();

?>

Was it helpful?

Solution

You can get the raw list of terms attached to a post with the get_the_terms() function. You can then use this to output their names without a link:

$my_categories = get_the_terms( null, 'my_category' );

if ( is_array( $my_categories ) ) {
    foreach ( $my_categories as $my_category ) {
        echo $my_category->name;
    }
}

To account for the possibility of multiple terms, you probably want to output the list of names separated by commas, in which case you could put the names into an array and implode() them:

$my_categories  = get_the_terms( null, 'my_category' );

if ( is_array( $my_categories ) ) {
    $category_names = wp_list_pluck( $my_categories, 'name' );

    echo implode( ', ', $category_names );
}

I'll leave the above, even though it turns out it's not relevant to OP, because it's still the correct answer to the original question as it appeared.

In regards to the updated question, if you want to output the name of a term given a specific slug, you need to retrieve that term with get_term_by() and then output it by echoing the name property of the resulting object.

<?php 

$args = array(
    'post_type' => 'my_post_type',
    'tax_query' => array(
         array(
            'taxonomy' => 'my_category',
            'field'    => 'slug',
            'terms'    => 'my_term',
         ),
    ),
);

$my_query = new WP_Query( $args );

$term = get_term_by( 'slug', 'my_term', 'my_category' );
?> 

<div>

    <h2><?php echo $term->name; ?></h2>

</div>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top