Skip to content

Instantly share code, notes, and snippets.

@imagebox
Created April 5, 2019 14:36
Show Gist options
  • Save imagebox/af56376004e4a4f86dfbf6fc56a4fbc5 to your computer and use it in GitHub Desktop.
Save imagebox/af56376004e4a4f86dfbf6fc56a4fbc5 to your computer and use it in GitHub Desktop.
[WordPress | Get the First Term] Get the first term on a single post by slug or name. #WordPress #cpt #taxonomy #term
<?php
// Get the first term on a single post by slug or name.
$tax = 'taxonomy_name';
$terms = get_the_terms( get_the_ID(), $tax );
$term_slug = array();
$term_name = array();
if ( $terms && ! is_wp_error( $terms )) {
foreach ( $terms as $term ) {
$term_slug[] = $term->slug;
$term_name[] = $term->name;
}
}
echo $term_slug[0];
echo $term_name[0];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment