Elementor 文章列表 卡片樣式 badge 顯示分類控制

在該分類下,顯示當下的分類 + 在非分類頁時顯示「主要的分類」

/**
 * Replace original archive card skin with skin that uses the primary tag set by yoast seo
 */
add_action( 'elementor/widget/archive-posts/skins_init', function ( $widget ) {

    if (!class_exists('\\WPSEO_Primary_Term')) {
        return;
    }

    class ArchiveCardSkinWithPrimaryTerm extends \ElementorPro\Modules\ThemeBuilder\Skins\Posts_Archive_Skin_Cards {
        protected function render_badge() {
            $taxonomy = $this->get_instance_value( 'badge_taxonomy' );
            if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) {
                return;
            }

            if (get_queried_object()->term_id) {
                $primary_term = get_term( get_queried_object()->term_id, $taxonomy );
            } else {
                $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 esc_html($primary_term->name); ?></div>
            <?php
        }
    }   

    // unregister the original archive cards skin including all hooks
    $original = $widget->get_skin( 'archive_cards' );  
    remove_action( 'elementor/element/archive-posts/section_layout/before_section_end', [ $original, 'register_controls' ] );
    remove_action( 'elementor/element/archive-posts/section_layout/after_section_end', [ $original, 'register_style_sections' ] );
    remove_action( 'elementor/element/archive-posts/archive_cards_section_design_image/before_section_end', [ $original, 'register_additional_design_image_controls' ] );
    $widget->remove_skin( 'archive_cards' );

    //reregister skin
    $widget->add_skin( new ArchiveCardSkinWithPrimaryTerm( $widget ) );
});