0

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!!

0

I found this and it works. Shout out to WP Munchies! https://wpmunchies.com/display-primary-category-using-yoasts-make-primary-feature/

<?php
// Fill in your custom taxonomy here
$yourTaxonomy = 'CUSTOM_TAXONOMY';
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_terms( $postId, $yourTaxonomy );
$useCatLink = true;
// If post has a category assigned.
if ($category){
    $category_display = '';
    $category_link = '';
    if ( class_exists('WPSEO_Primary_Term') )
    {
        // Show the post's 'Primary' category, if this Yoast feature is available, & one is set
        $wpseo_primary_term = new WPSEO_Primary_Term( 'event_cat', get_the_id() );
        $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
        $term = get_term( $wpseo_primary_term );
        if (is_wp_error($term)) { 
            // Default to first category (not Yoast) if an error is returned
            $category_display = $category[0]->name;
            $category_link = get_bloginfo('url') . '/' . 'event-category/' . $term->slug;
        } else { 
            // Yoast Primary category
            $category_display = $term->name;
            $category_link = get_term_link( $term->term_id );
        }
    } 
    else {
        // Default, display the first category in WP's list of assigned categories
        $category_display = $category[0]->name;
        $category_link = get_term_link( $category[0]->term_id );
    }
    // Display category
    if ( !empty($category_display) ){
        if ( $useCatLink == true && !empty($category_link) ){
        echo '<span class="post-category">';
        echo '<a href="'.$category_link.'">'.$category_display.'</a>';
        echo '</span>';
        } else {
        echo '<span class="post-category">'.$category_display.'</span>';
        }
    }

} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.