Archived topic
Showing only one Primary category
15 replies · Started by Ahmed Eid on January 18, 2020
Hello,
How can i display only one post category for each post On page hero
. We are using Rank Math SEO that create Primary Category.
Thanks!
Hi there,
You will likely need to create a shortcode with some PHP to get the primary category.
Let's try this:
add_shortcode( 'primary-category', function() {
$cat = false;
$primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
if ( $primary_cat_id ) {
$cat = get_term( $primary_cat_id, 'category' );
$cat_link = get_term_link( $primary_cat_id, 'category' );
}
if ( ! is_wp_error( $cat ) && ! is_wp_error( $cat_link ) ) {
return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';
}
} );
Then add the [primary-category] shortcode into the Hero.
Let me know :)
Thanks for the help "Tom"
Unfortunately the shortcode does not work
Hi there,
Does nothing output at all?
What if you do this?:
add_shortcode( 'primary-category', function() {
$product_cat = false;
$primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
if ( $primary_cat_id ) {
$product_cat = get_term( $primary_cat_id, 'product_cat' );
}
var_dump($product_cat);
if ( $product_cat ) {
return '<span class="primary-category">' . $product_cat->name . '</span>';
}
} );
This error occurs
object (WP_Error) # 5759 (2) {["errors"] => array (1) {["invalid_taxonomy"] => array (1) {[0] => string (25) "invalid class." }} ["error_data"] => array (0) {}}
Aha, that explains it.
Try this updated code: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469
Great works well
But it cannot be clicked as a link
Also
But css cannot be implemented
I just updated the function to include the link: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469
You can use this class to style it: .primary-category
Sorry Tom
But the error appears
PHP Recoverable fatal error: Object of class WP_Error could not be converted to string
On the next line:
return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';
Hmm, one more debug try:
add_shortcode( 'primary-category', function() {
$cat = false;
$primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
if ( $primary_cat_id ) {
$cat = get_term( $primary_cat_id, 'category' );
$cat_link = get_term_link( $primary_cat_id, 'category' );
}
var_dump($cat_link);
if ( $cat ) {
return '<span class="primary-category"><a href="#">' . $cat->name . '</a></span>';
}
} );
What does that var_dump output? It should get the link to the term..
Let me know :)
Unfortunately, this error occurred
object (WP_Error) # 5775 (2) {["errors"] => array (1) {["invalid_term"] => array (1) {[0] => string (18) "is an empty element." }} ["error_data"] => array (0) {}}
Ok, can you try this?: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469
If that doesn't work I'll install Rank Math and will debug.
Thanks!
Sorry, Tom, you are tired of me
It didn't work at all
No worries!
Just played with it - this should work:
add_shortcode( 'primary-category', function() {
$cat = false;
$primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
if ( $primary_cat_id ) {
$cat = get_term( $primary_cat_id, 'category' );
$cat_link = get_term_link( $cat, 'category' );
}
if ( ! is_wp_error( $cat ) ) {
return '<span class="primary-category"><a href="' . $cat_link . '">' . $cat->name . '</a></span>';
}
} );
Thank you Tom
It works perfectly
good job