0

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();

?>

1

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>
  • Thank you, there isn't the possibility for multiple terms in this case; I'm trying with <h2><?php echo get_the_terms( 0, 'my_category')->name; ?></h2>, but I'm getting this error Notice: Trying to get property of non-object in {source file} – Sekhemty Feb 20 at 10:50
  • Even if there's only 1 term, get_the_terms() still returns an array. – Jacob Peattie Feb 20 at 10:51
  • I've tried both of your solutions and they are not working for me. In both cases I get an invalid argument error. – Sekhemty Feb 20 at 11:03
  • Are you substituting my_category for your actual taxonomy name? – Jacob Peattie Feb 20 at 11:05
  • Yes I wrote my_category here just to generalize, in my template file I'm actually using the real name. – Sekhemty Feb 20 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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