Yoast – Custom Taxonomy – primary category

I have a Custom Taxonomy with the name country. I would like to display the primary category only. I have the yoast plugin installed and that is how I’m setting the primary category.

I’m using this code I found to do the same thing but with post categories and it works well.

<?php
$primaryCat = new WPSEO_Primary_Term(‘category’, get_the_ID());
$primaryCat = $primaryCat->get_primary_term();
$primaryCat = get_cat_name($primaryCat);

$categories = get_the_category();
foreach( $categories as $category ) {
$defaultCat = $category->name;
$defaultCatLink = get_category_link( $category->term_id );
}

if ( $primaryCat !== “” ) {
$cat = new WPSEO_Primary_Term(‘category’, get_the_ID());
$cat = $cat->get_primary_term();

$catName = get_cat_name($cat);
$catLink = get_category_link($cat);

} else {
$catName = $defaultCat;
$catLink = $defaultCatLink;
}
?>

<h3><a href=”<?php echo $catLink; ?>”><?php echo $catName; ?></a></h3>

I would be very grateful if someone could help me figure out how to get the primary category and display it for a custom taxonomy?

Thank you for taking the time to look at my problem!!

Read more here:: Yoast – Custom Taxonomy – primary category

Leave a Reply

Your email address will not be published. Required fields are marked *