How to display the custom post’s selected custom taxonomy inside the loop without the link and echo?

Here’s the code snippet that I am trying to sort out:

$args=array(
      'post_type' => 'custom_post_type',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'meta_key'         => 'custom_meta_key',
      'meta_value'       => 'on',
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    while ( $my_query->have_posts() ) : $my_query->the_post();
        $custom_taxonomy = the_terms( $post->ID, 'custom_taxonomy');
    endwhile;

however, this echoes

Read More
<a rel="tag" href="http://myweb/custom_taxonomy/selectedTerm/">selectedTerm</a>

but I only need the selectedTerm

Using strip_tags() doesn’t help since the_terms() is echoing the link.

Related posts

Leave a Reply

1 comment

  1. You will want to use get_the_terms( $post->ID, 'custom_taxonomy' ) instead of the_terms()

    This will return an array of term objects. You can access the term names by doing the following:

    while ( $my_query->have_posts() ) : $my_query->the_post();
        $custom_taxonomy = the_terms( $post->ID, 'custom_taxonomy');
    endwhile;
    
    // Print the term names
    foreach ( $custom_taxonomy as $term ) {
      echo $term->name;
    }
    

    Check out the codex for more information on get_the_terms()