Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns the primary term for the chosen taxonomy set by Yoast SEO or the first term selected.
<?php
/**
* Returns the primary term for the chosen taxonomy set by Yoast SEO
* or the first term selected.
*
* @link https://www.tannerrecord.com/how-to-get-yoasts-primary-category/
* @param integer $post The post id.
* @param string $taxonomy The taxonomy to query. Defaults to category.
* @return array The term with keys of 'title', 'slug', and 'url'.
*/
function get_primary_taxonomy_term( $post = 0, $taxonomy = 'category' ) {
if ( ! $post ) {
$post = get_the_ID();
}
$terms = get_the_terms( $post, $taxonomy );
$primary_term = array();
if ( $terms ) {
$term_display = '';
$term_slug = '';
$term_link = '';
if ( class_exists( 'WPSEO_Primary_Term' ) ) {
$wpseo_primary_term = new WPSEO_Primary_Term( $taxonomy, $post );
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$term = get_term( $wpseo_primary_term );
if ( is_wp_error( $term ) ) {
$term_display = $terms[0]->name;
$term_slug = $terms[0]->slug;
$term_link = get_term_link( $terms[0]->term_id );
} else {
$term_display = $term->name;
$term_slug = $term->slug;
$term_link = get_term_link( $term->term_id );
}
} else {
$term_display = $terms[0]->name;
$term_slug = $terms[0]->slug;
$term_link = get_term_link( $terms[0]->term_id );
}
$primary_term['url'] = $term_link;
$primary_term['slug'] = $term_slug;
$primary_term['title'] = $term_display;
}
return $primary_term;
}
@tarecord

This comment has been minimized.

Copy link
Owner Author

@tarecord tarecord commented Jan 5, 2020

Sample Usage

Display the primary category from the default WordPress category taxonomy

<?php
    // Within The Loop.
    $primary_category = get_primary_taxonomy_term();
?>

<div class="category">
    <?php echo  $primary_category['title']; ?>
</div>

Display the primary term from a custom taxonomy

<?php
    // Within The Loop so $post is the current post object.
    $primary_category = get_primary_taxonomy_term( $post, 'my-custom-taxonomy-slug' );
?>

<div class="category">
    <?php echo $primary_category['title']; ?>
</div>

Add a link to the primary category's archive page

<?php
    // Within The Loop.
    $primary_category = get_primary_taxonomy_term();
?>

<a class="category" href="<?php echo $primary_category['url']; ?>">
    <?php echo $primary_category['title']; ?>
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment