get_the_terms()

This core WordPress function works with CPT-onomies and DOES NOT require the CPT-onomy class.

Description

Retrieves the terms of the CPT-onomy that are attached to the post.

Usage

get_the_terms( 						int
						$post_id
						, 						string
						$cpt_onomy
						 );

Parameters

$post_id (integer) (required)
Default: none
The post ID.

$cpt_onomy (string) (required)
Default: none
Name of the CPT-onomy to retrieve terms from.

Return Values

(array|false|WP_Error) Array of term objects on success, false if no terms are found in the given $cpt_onomy, and a WP_error object if an invalid $cpt_onomy is entered.

Term object fields:

  • term_id - which is the same as the post ID
  • name
  • slug
  • term_group
  • term_taxonomy_id
  • taxonomy
  • description
  • parent
  • count

Examples

<?php

$terms = get_the_terms( $post->ID, 'actors' );

if ( $terms && ! is_wp_error( $terms ) ) :

   $actors = array();

   foreach ( $terms as $term ) {
      $actors[] = $term->name;
   }

   $actors = join( ", ", $actors );

   ?>

   <p>Actors: <span><?php echo $actors; ?></span></p>

<?php endif; ?>

WordPress Codex

For more information, refer to the WordPress codex.