Trying to get primary category to show on Elementor Posts badge
-
Hey all,
I absolutely love TSF plugin, it’s now my go-to SEO plugin for WordPress! For the past week, I’ve just been struggling with one small issue on my website.
I’m using the Elementor Posts widget to display posts in a grid and also using the badge feature (category taxonomy). Unfortunately, this causes the badge to display the first category alphabetically if a post is in multiple categories. I’d rather the primary category, as selected by TSF, to be the only one displayed on the badge.
I’ve found the following code which does exactly this, but sadly it’s for Yoast SEO. I was wondering if anyone would be able to modify this to work with TSF?
/** * Replace original card skin with skin that uses the primary tag set by yoast seo */ add_action( 'elementor/widget/posts/skins_init', function( $widget ) { if (!class_exists('\\WPSEO_Primary_Term')) { return; } class CardSkinWithPrimaryTerm extends \ElementorPro\Modules\Posts\Skins\Skin_Cards { protected function render_badge() { $taxonomy = $this->get_instance_value( 'badge_taxonomy' ); if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) { return; } $primary_term = new \WPSEO_Primary_Term( $taxonomy, get_the_ID() ); if ( ! $primary_term || ! $primary_term->get_primary_term() ) { return parent::render_badge(); } $primary_term = get_term( $primary_term->get_primary_term(), $taxonomy ); ?> <div class="elementor-post__badge"><?php echo $primary_term->name; ?></div> <?php } } // unregister the original cards skin including all hooks $original = $widget->get_skin( 'cards' ); remove_action( 'elementor/element/posts/section_layout/before_section_end', [ $original, 'register_controls' ] ); remove_action( 'elementor/element/posts/section_query/after_section_end', [ $original, 'register_style_sections' ] ); remove_action( 'elementor/element/posts/cards_section_design_image/before_section_end', [ $original, 'register_additional_design_image_controls' ] ); $widget->remove_skin( 'cards' ); //reregister skin $widget->add_skin( new CardSkinWithPrimaryTerm( $widget ) ); } );
Thanks so much for your time! The website is https://easywithai.com if it helps.
The page I need help with: [log in to see the link]
- The topic ‘Trying to get primary category to show on Elementor Posts badge’ is closed to new replies.