- This topic has 15 replies, 2 voices, and was last updated 4 years, 2 months ago by Tom.
-
AuthorPosts
-
January 18, 2020 at 6:12 am #1135926Ahmed EidJanuary 18, 2020 at 5:53 pm #1136469TomLead DeveloperLead Developer
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 ๐
January 19, 2020 at 4:43 am #1136712Ahmed EidThanks for the help “Tom”
Unfortunately the shortcode does not workJanuary 19, 2020 at 9:29 am #1137053TomLead DeveloperLead DeveloperHi 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>'; } } );
January 19, 2020 at 10:14 am #1137084Ahmed EidThis error occurs
object (WP_Error) # 5759 (2) {["errors"] => array (1) {["invalid_taxonomy"] => array (1) {[0] => string (25) "invalid class." }} ["error_data"] => array (0) {}}
January 19, 2020 at 4:26 pm #1137319TomLead DeveloperLead DeveloperAha, that explains it.
Try this updated code: https://generatepress.com/forums/topic/showing-only-one-primary-category/#post-1136469
January 20, 2020 at 5:58 am #1137784Ahmed EidGreat works well
But it cannot be clicked as a link
Also
But css cannot be implementedJanuary 20, 2020 at 10:54 am #1138269TomLead DeveloperLead DeveloperI 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
January 20, 2020 at 1:01 pm #1138362Ahmed EidSorry 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>';
January 20, 2020 at 6:32 pm #1138546TomLead DeveloperLead DeveloperHmm, 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 ๐
January 20, 2020 at 8:22 pm #1138607Ahmed EidUnfortunately, 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) {}}
January 21, 2020 at 8:05 am #1139307TomLead DeveloperLead DeveloperOk, 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!
January 21, 2020 at 9:45 am #1139427Ahmed EidSorry, Tom, you are tired of me
It didn’t work at allJanuary 21, 2020 at 3:52 pm #1139706TomLead DeveloperLead DeveloperNo 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>'; } } );
January 21, 2020 at 5:02 pm #1139759Ahmed EidThank you Tom
It works perfectly
good job -
AuthorPosts
- You must be logged in to reply to this topic.