Skip to content

Instantly share code, notes, and snippets.

@jawinn
Last active December 8, 2022 21:42
Show Gist options
  • Save jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c to your computer and use it in GitHub Desktop.
Save jawinn/1b44bf4e62e114dc341cd7d7cd8dce4c to your computer and use it in GitHub Desktop.
Display Primary Category (Yoast's WordPress SEO)
<?php
/**
* Get the Yoast primary category from its post meta value, and displays it, with HTML markup.
* If there is no primary category set, it displays the first assigned category.
*
* @param boolean $useCatLink Whether to link the category, if it exists
* @return void
*/
function yourtheme_display_yoast_primary_category( $useCatLink = true ) {
// Retrieves post categories
$category = get_the_category();
// If post has a category assigned.
if ( $category ) {
$category_display = '';
$category_link = '';
// Get post's 'Primary' category from post meta
$yoast_primary_key = get_post_meta( get_the_id(), '_yoast_wpseo_primary_category', TRUE );
if ( !empty($yoast_primary_key) )
{
$term = get_term( $yoast_primary_key );
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's 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="', esc_url($category_link), '">', esc_html_e($category_display), '</a>';
echo '</span>';
} else {
echo '<span class="post-category">', esc_html_e($category_display), '</span>';
}
}
}
}
?>
@Ikubod
Copy link

Ikubod commented Dec 28, 2016

Saved my tush. Thanks for this!

@sawwd
Copy link

sawwd commented Mar 13, 2017

Thank you :)

@brainbrian
Copy link

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
Copy link

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

@MathieuLopes
Copy link

Thank you!
Great job!

@zenaul
Copy link

zenaul commented May 21, 2018

It's helped me out.
Thank you :)

@sakrow
Copy link

sakrow commented Jun 13, 2018

Great Job, more useful!

@ultrinnan
Copy link

thank you!

@apmeyer
Copy link

apmeyer commented Sep 17, 2018

Thanks for this!

@JiveDig
Copy link

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
Copy link

abidr commented Nov 7, 2018

Thanks, man.

@MichaelSmeal
Copy link

This worked great! Thank you much!

@madushanl
Copy link

Thank mate!

@highsmithcodes
Copy link

Life saver.

@akshay-sihag
Copy link

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
Copy link

Tks mate!! :)

@hamilton-seguin
Copy link

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

@shzmnz
Copy link

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!

@jawinn
Copy link
Author

jawinn commented Mar 15, 2022

Just updated the script with a new an improved version for 2022, since this has been pretty popular and it needed a few adjustments. The primary meta key is now retrieved via the post meta value, which is a little more futureproof. It's now a function with a param for showing the link or not. And both the URL and category name are now escaped with the WP functions esc_url and esc_html_e (displays symbols like "&" properly as discussed in the comments here).

@AlphaBlossom
Copy link

Thanks so much for this, getting me close to where I need to be. It's displaying the Primary Category (from the dropdown settings) correctly but not displaying the link...any ideas what could be this issue?

@AlphaBlossom
Copy link

got it, I had to add $useCatLink = "true"; and worked great (thought only needed that to NOT display the link).

Thanks again, such a huge help!

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