Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Display Primary Category (Yoast's WordPress SEO)
<?php
// SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
$category = get_the_category();
$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( 'category', 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_category_link( $category[0]->term_id );
} else {
// Yoast Primary category
$category_display = $term->name;
$category_link = get_category_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_category_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.'">'.htmlspecialchars($category_display).'</a>';
echo '</span>';
} else {
echo '<span class="post-category">'.htmlspecialchars($category_display).'</span>';
}
}
}
?>
@Ikubod

This comment has been minimized.

Copy link

@Ikubod Ikubod commented Dec 28, 2016

Saved my tush. Thanks for this!

@sawwd

This comment has been minimized.

Copy link

@sawwd sawwd commented Mar 13, 2017

Thank you :)

@brainbrian

This comment has been minimized.

Copy link

@brainbrian brainbrian commented Jun 1, 2017

I'm surprised Yoast doesn't reorder the array and just make the primary the first item in the array. It seems to take when using categories in post URLs but not when doing a standard get_the_category.

Thanks for this gist!

@anastahery

This comment has been minimized.

Copy link

@anastahery anastahery commented Jun 18, 2017

how can i display a parent category of the “Primary” Category??

@MathieuLopes

This comment has been minimized.

Copy link

@MathieuLopes MathieuLopes commented Dec 18, 2017

Thank you!
Great job!

@zenaul

This comment has been minimized.

Copy link

@zenaul zenaul commented May 21, 2018

It's helped me out.
Thank you :)

@sakrow

This comment has been minimized.

Copy link

@sakrow sakrow commented Jun 13, 2018

Great Job, more useful!

@ultrinnan

This comment has been minimized.

Copy link

@ultrinnan ultrinnan commented Jul 16, 2018

thank you!

@apmeyer

This comment has been minimized.

Copy link

@apmeyer apmeyer commented Sep 17, 2018

Thanks for this!

@JiveDig

This comment has been minimized.

Copy link

@JiveDig JiveDig commented Oct 17, 2018

Thanks for the head start! I converted it to a helper function that just returns the term object. Now I can use it with any taxonomy and do and build the markup however I want with the data.
https://gist.github.com/JiveDig/5d1518f370b1605ae9c753f564b20b7f

@abidr

This comment has been minimized.

Copy link

@abidr abidr commented Nov 7, 2018

Thanks, man.

@mk1sml

This comment has been minimized.

Copy link

@mk1sml mk1sml commented Mar 25, 2019

This worked great! Thank you much!

@madushanl

This comment has been minimized.

Copy link

@madushanl madushanl commented Apr 18, 2019

Thank mate!

@highsmithcodes

This comment has been minimized.

Copy link

@highsmithcodes highsmithcodes commented Oct 2, 2019

Life saver.

@geekysihag

This comment has been minimized.

Copy link

@geekysihag geekysihag commented Jan 14, 2020

Thanks for this, with the help of this i was able to create a shortcode which can display the Primary Category used by Yoast wherever you want

https://gist.github.com/geekysihag/97938e6e8e318d0ab59b437f44b8b6f1

@felipebuchmann

This comment has been minimized.

Copy link

@felipebuchmann felipebuchmann commented Oct 14, 2020

Tks mate!! :)

@VoyageinStyle

This comment has been minimized.

Copy link

@VoyageinStyle VoyageinStyle commented Mar 26, 2021

thanks a lot for that, I've just changed the
htmlspecialchars to htmlspecialchars_decode as it was not treating a "&" properly!

@shzmnz

This comment has been minimized.

Copy link

@shzmnz shzmnz commented Jun 30, 2021

thanks a lot for that, I've just changed the
htmlspecialchars to htmlspecialchars_decode as it was not treating a "&" properly!

You absolute legend. I was hoping someone would have the answer to that problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment