Showing Clickable Primary Category with a Shortcode
-
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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Showing Clickable Primary Category with a Shortcode’ is closed to new replies.