get_terms()

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

Description

Retrieves the terms in your CPT-onomy or list of CPT-onomies.

Usage

get_terms( 						string|array
						$cpt_onomies
						, 						string|array
						$args
						 );

Parameters

$cpt_onomies (string|array) (required)
Default: none
Name of the CPT-onomy to retrieve terms from. Can include multiple CPT-onomies.

$args (string|array) (optional)
Default: none
Arguments to customize what data is returned.
For information on how to customize the $args parameter, refer to the WordPress codex.

Return Values

(array|WP_error) Array of term objects on success, an empty array if no terms are found, 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

String syntax:

<?php $actors = get_terms( 'actors', 'orderby=count&hide_empty=0' ); ?>

Array syntax:

<?php

$actors = get_terms( 'actors', array(
   'orderby' => 'count',
   'hide_empty' => 0
) );

?>

List all the terms in your CPT-onomy, without a link:

<?php

$terms = get_terms( 'actors' );
if ( $terms && !is_wp_error( $terms ) ) {
   echo '<ul>';
   foreach ( $terms as $term ) {
      echo '<li>' . $term->name . '</li>';
   }
   echo '</ul>';
}

?>

List all the terms in your CPT-onomy, with a link to the term's archive page: Note: You must use the $cpt_onomy class to retrieve the term's archive link ( get_term_link() ).

<?php

global $cpt_onomy;
$terms = get_terms( 'actors' );
if ( $terms && !is_wp_error( $terms ) ) {
   echo '<ul>';
   foreach ( $terms as $term ) {
      echo '<li><a href="' . $cpt_onomy->get_term_link( $term, $term->taxonomy ) . '">' . $term->name . '</a></li>';
   }
   echo '</ul>';
}

?>

List all the terms in your CPT-onomy, with a link to the term's post permalink:

<?php

$terms = get_terms( 'actors' );
if ( $terms && !is_wp_error( $terms ) ) {
   echo '<ul>';
   foreach ( $terms as $term ) {
      echo '<li><a href="' . get_permalink( $term->term_id ) . '">' . $term->name . '</a></li>';
   }
   echo '</ul>';
}

?>

WordPress Codex

For more information, refer to the WordPress codex.