Wordpress-seo: Template tag to get primary category

Created on 1 Mar 2016  路  14Comments  路  Source: Yoast/wordpress-seo

Well, yeah. That.

yoast_seo_primary_term() ofzo.

primary category feature request

Most helpful comment

I ended up using this:

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

Hopefully that will help out a little?

All 14 comments

:+1:

for the people who want to do this now, this worked for me:

    $cat = new WPSEO_Primary_Term('category', get_the_ID());
    $cat = $cat->get_primary_term();

It would be great if we could apply this to taxonomies as well :+1: the UI in the backend doesnt seem to work on taxonomies.

Would also be great to have it hook into get_the_category() to make sure the first object in the array is always the primary taxonomy; overridable by a filter or so!

Thanks by the way, nice feature.

@qikkeronline Cheers for dropping the lookup in here - do you know if its possible to display the cat name rather than the ID?

@sgirjon
on the next line:

$cat = get_category($cat); // this will contain all category data, including the name
var_dump($cat); // find out how to get the title, probably sth like $cat->name or $cat->term_name or so (no time to look it up for you, soz) 

Thanks for taking a look @qikkeronline - it seems to not want to display the category name still - does anyone know if this is just a missing feature or just that I'm missing something?

@sgirjon can you drop your code (the entire php template) here? Might have to do with your loop or so.

Just if anyone stumbles upon this to try and get the primary category name (and the category link as well)... This got it working for me:

$cat = new WPSEO_Primary_Term('category', get_the_ID());
$cat = $cat->get_primary_term();
$catName = get_cat_name($cat);
$catLink = get_category_link($cat);
echo $catName;
echo $catLink;

Is there a way to do this with custom taxonomies? Seems to be just returning blank after the $cat->get_primary_term(). I did change 'category' to my custom taxonomies name.

Nevermind, it doesn't return anything if you don't set a primary category 馃槕

I ended up using this:

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

Hopefully that will help out a little?

Is there a way to do this with custom categories?? None of the above answers have worked for me and desperately need this function!!

@ThomasHoadley you could try and use get_terms() to retrieve custom taxonomies instead of get_the_category() when declaring the $categories variable. They both return the WP_Term object, so you might be in good shape. https://developer.wordpress.org/reference/functions/get_terms/

the below code worked for me to get the primary category that is set up in the admin section

$primary_cat_id=get_post_meta($product->id,'_yoast_wpseo_primary_product_cat',true);
if($primary_cat_id){
$product_cat = get_term($primary_cat_id, 'product_cat');
if(isset($product_cat->name))
echo $product_cat->name;
}

So the code I posted above was just working on categories; this will do the same for any taxonomy:

$wpseo_primary_term = new WPSEO_Primary_Term('your_taxonomy_name', $your_post_ID);
$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
$wpseo_primary_term = get_term($wpseo_primary_term);

At this point the $wpseo_primary_term variable contains the entire term object (see https://codex.wordpress.org/Function_Reference/get_term#Return_Values) of the primary term of your post, and values can be retrieved like this:

$wpseo_primary_term->term_id;
$wpseo_primary_term->name;
$wpseo_primary_term->parent;
Was this page helpful?
0 / 5 - 0 ratings