Created
April 5, 2019 14:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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