Site logo

[Resolved] Display primary category in dynamic list of terms

Home Forums Support [Resolved] Display primary category in dynamic list of terms

Home Forums Support Display primary category in dynamic list of terms

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #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!
    Becki

    #2491388
    David
    Staff
    Customer Support

    Hi there,

    could i see the site ?

    #2491417
    Rebecca

    Yes! pasting credentials in private area

    #2491882
    Ying
    Staff
    Customer Support

    Hi Rebecca,

    Can you share the shortcode function as well?

    #2491937
    Rebecca

    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' );
    
    #2491949
    Ying
    Staff
    Customer Support

    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.

    #2492077
    Rebecca

    Hi Ying,

    This worked, thanks so much!

    #2492105
    Ying
    Staff
    Customer Support

    Glad to hear that 🙂

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.