• Resolved swiftbowl

    (@swiftbowl)


    Hi there.

    We love using TSF, especially compared to other solutions out there.

    We are trying to show a primary category as a clickable link using a shortcode.

    Below is the code we’ve used with YOAST primary category feature.
    Any suggestions on how to make it work with TSF?

    function dynamic_category_link_function( $atts ) {
    	if ( in_array( get_post_type(), array('posts','test'))){
    		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 '<a class="gb-headline-text" href="' . $category_link . '">' . $term_name .'</a>';
    	
    	return ob_get_clean();
    	
    }
    add_shortcode( 'dynamic_category_link', 'dynamic_category_link_function' );

    Any help would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter swiftbowl

    (@swiftbowl)

    Thanks

    Plugin Author Sybre Waaijer

    (@cybr)

    Hello!

    That shortcode is not secure (output escaping is lacking). It also needlessly uses output buffering.

    I made you a new one that works with any (custom) post type, and takes the first term found if TSF is deactivated (or when no primary term is entered). See https://gist.github.com/sybrew/08b17dd3249139a0cf0d350600af0591.

    I also changed the shortcode name to reflect these changes. So, you need to use [dynamic_term_link].

    Cheers ??

    P.S. if you still want to use [dynamic_category_link], simply add this line above the other add_shortcode call — you’ll then have two shortcodes using the same function but that’s perfectly OK:

    add_shortcode( 'dynamic_category_link', 'dynamic_term_link_function' );
    
    • This reply was modified 2 years, 4 months ago by Sybre Waaijer. Reason: PS
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Showing Clickable Primary Category with a Shortcode’ is closed to new replies.