- This topic has 16 replies, 6 voices, and was last updated 2 years, 8 months ago by
Leo.
-
AuthorPosts
-
December 6, 2022 at 7:47 am #2451519
Cheryl
Hello,
In another post somewhere here, I found this:
add_shortcode( 'display_category', function() { // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY $category = get_the_category(); // If post has a category assigned. if ($category){ $category_display = ''; 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; } else { // Yoast Primary category $category_display = $term->name; } } else { // Default, display the first category in WP's list of assigned categories $category_display = $category[0]->name; } // Display category return $category_display; } } );
…which works great to get the post category name. What is the right way to modify this to get the primary category name of a custom post type?
For instance if I have a category for events called event-category, how would I modify this to get the primary event-category?Thanks
December 6, 2022 at 9:11 am #2451729David
StaffCustomer SupportHi there,
probably like this:
add_shortcode( 'display_category', function() { // set your taxonomy name $tax_name = 'your_tax_name'; // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY $category = get_terms( array( 'taxonomy' => $tax_name, ) ); // If post has a category assigned. if ($category){ $category_display = ''; 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( $tax_name , 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; } else { // Yoast Primary category $category_display = $term->name; } } else { // Default, display the first category in WP's list of assigned categories $category_display = $category[0]->name; } // Display category return $category_display; } } );
Just update this variable:
$tax_name = 'your_tax_name';
December 6, 2022 at 11:07 am #2451844Cheryl
Thanks! That works great.
I’m also using:/*dynamic category link shortcode*/ function dynamic_category_link_function( $atts ) { ob_start(); $categories = get_the_category(); $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_link = get_category_link( $categories[0]->term_id ); } else { // Yoast Primary category $category_link = get_category_link( $term->term_id ); } } else { // Default, display the first category in WP's list of assigned categories $category_link = get_category_link( $categories[0]->term_id ); } echo $category_link; return ob_get_clean(); } add_shortcode( 'dynamic_category_link', 'dynamic_category_link_function' );
to get the category link for regular posts. But I’m having difficulty with modifying it to get the dynamic category link for custom post types. Any suggestions there?
ThanksDecember 6, 2022 at 12:04 pm #2451923Cheryl
…Though I’m finding that at least with the event category, if there isn’t a “primary” category assigned, somehow, it’s fetching an entirely unrelated category.
December 6, 2022 at 6:22 pm #2452264Fernando Customer Support
Hi Cheryl,
That should work for Custom Post types as well. I also tested it on my test site, and it’s working. It’s getting the Primary Category Link.
Or, are you referring to something different when you say “Dynamic Category Link”?
What “event” category is this? I believe Yoast should select a Primary Category by default. It shouldn’t be unassigned.
December 6, 2022 at 8:34 pm #2452350Cheryl
It works fine for regular posts, but on custom events, the link is to the page itself. I was using events as an example. I have WP Events Manager and was trying to get the dynamic link for the event category (the code that David posted previously works fine for getting the category name). But the behavior is the same for other custom post types.
In the case of the primary category, there’s a link when you’re selecting a category to “make primary.” At least for events I found that I had an event for which I hadn’t selected the primary category and it showed a completely unrelated category until I manually chose the primary category. Then it was fine. Note that I had categorized it, just hadn’t selected any category as “primary.”
Thanks
December 6, 2022 at 9:01 pm #2452358Fernando Customer Support
I see. that’s odd. It should work for Custom Post Types.
Can you provide the link to a specific “event” where the issue is evident?
You may use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
December 6, 2022 at 9:26 pm #2452368Cheryl
OK, I added a link in the private field. Here’s what I have in the header I’m setting up, where [display_event_category] is my revision of David’s snippet above to get the name of the event category.
But for categories, I have something like “Arts” and “Theater” selected, and as you can see the category it’s getting is Agricultural Shows & Tours which isn’t even selected as a category. I think it’s choosing the first category alphabetically if I don’t have a primary selected. If I go in and click the “Make Primary” link in the editor for the “Theater” category, then it will show that category.
As for the link, the [dynamic_category_link] works great for regular posts, but here and other custom post types it’s returning the URL of the page itself.
Thanks
December 6, 2022 at 10:37 pm #2452425Fernando Customer Support
I see. Let’s try to tackle the link first.
Are you using a different code for the link?
Can you try this exact code first?: https://generatepress.com/forums/topic/get-yoast-primary-category-in-shortcode-for-custom-post-type/#post-2451844
It should just echo out the URL.
December 7, 2022 at 8:46 am #2453325Cheryl
Hi,
That redirects me back to the code I entered above. That is the exact code I’m using to get the link for the category. For that I only have the one. I customized the other code for specific custom post types.
ThanksDecember 7, 2022 at 5:22 pm #2453854Fernando Customer Support
I see. Can you provide admin login credentials so we can take a closer look at your setup?
Please use the Private Information field for this: https://docs.generatepress.com/article/using-the-premium-support-forum/#private-information
January 30, 2023 at 10:44 am #2514505Jacopo
Hi David, Fernando,
thanks for your amazing support! I have a similar problem: I would like to show only the primary taxonomy “breeds” from Yoast for the custom post type “dogs”. On the frontend I would like the primary taxonomy to be clickable and link back to its corresponding taxonomy repository.
I tried the code as it is now but it doesn’t work.
January 30, 2023 at 1:51 pm #2514701Ying
StaffCustomer SupportHi Jacopo,
I would like to show only the primary taxonomy “breeds” from Yoast for the custom post type “dogs”.
Does Yoast SEO have the primary taxonomy option? I don’t know about that.
Can you show us the settings by taking a screenshot?January 30, 2023 at 5:23 pm #2514840Jacopo
Hi,
Of course Yoast has it, just like any categories 🙂
January 30, 2023 at 5:36 pm #2514850Leo
StaffCustomer SupportHi there,
Would you be able to check with Yoast’s support team for this request as it’s specifically related to their product?
The questions here are very out of the scope of what we can support here:
https://generatepress.com/what-support-includes/If Yoast’s support team cannot help then a WP general forum like this might be your best bet:
https://wordpress.stackexchange.com/Thanks for your understanding 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.