Site logo

Archived topic

Display primary category in dynamic list of terms

7 replies · Started by Rebecca on January 11, 2023

Viewing posts 1–8 of 8

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!
Becki

Hi there,

could i see the site ?

Yes! pasting credentials in private area

Hi Rebecca,

Can you share the shortcode function as well?

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' );

Hi 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.

Hi Ying,

This worked, thanks so much!

Glad to hear that :)

This archived topic is closed to new replies.