- This topic has 7 replies, 3 voices, and was last updated 3 years, 3 months ago by
Ying.
-
AuthorPosts
-
January 11, 2023 at 4:29 am #2491223
Rebecca
Hello! I’ve been using a function I found here to create a shortcode for primary category. This has been working well to display the Yoast primary category on archive pages. I add the shortcode to a button to achieve this.
I’m trying to redesign my homepage and it’s not working there. The process is a little different – I’m using dynamic data and ‘list of terms’ to pull in the categories. However I only want to display the primary category, and the shortcode isn’t working using either a button or headline. Any ideas how I could achieve this?
I have this on staging right now – happy to provide a demo link if needed.
Thanks!
BeckiJanuary 11, 2023 at 6:48 am #2491388David
StaffCustomer SupportHi there,
could i see the site ?
January 11, 2023 at 7:12 am #2491417Rebecca
Yes! pasting credentials in private area
January 11, 2023 at 12:02 pm #2491882Ying
StaffCustomer SupportHi Rebecca,
Can you share the shortcode function as well?
January 11, 2023 at 12:46 pm #2491937Rebecca
Sure! here it is:
function primary_category_function( $atts ) { if ( 'post' !== get_post_type()|| 'resorts' === get_post_type() ) { return; } 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 ); $term_name = get_term( $categories[0]->term_id )->name; } else { // Yoast Primary category $category_link = get_category_link( $term->term_id ); $term_name = get_term( $term->term_id )->name; } } else { // Default, display the first category in WP's list of assigned categories $category_link = get_category_link( $categories[0]->term_id ); $term_name = get_term( $categories[0]->term_id )->name; } echo '<span class="primary-category"><a href="' . $category_link . '">' . $term_name .'</a>'; return ob_get_clean(); } add_shortcode( 'primary-category', 'primary_category_function' );January 11, 2023 at 1:07 pm #2491949Ying
StaffCustomer SupportHi Rebecca,
Try using headline block to output the shortcode, please follow the steps:
1. Add a headline block, add a custom CSS class to it, eg.
primary-category.2. Add this PHP snippet:
add_filter( 'render_block', function( $block_content, $block ) { if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'primary-category' ) !== false ) { $block_content = do_shortcode('[primary-category]'); } return $block_content ; }, 10, 2 );Let me know if this works.
January 11, 2023 at 3:17 pm #2492077Rebecca
Hi Ying,
This worked, thanks so much!
January 11, 2023 at 4:05 pm #2492105Ying
StaffCustomer SupportGlad to hear that 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.