使用自定义皮肤扩展Elementor归档帖子小部件,以显示多个徽章

时间:2021-04-24 作者:BarrieO

我在Elementor中使用自定义外观修改了我的posts小部件,以便显示multiple badges 代码如下:

/* MULTIPLE BADGES ON POST */
/* --- */
add_action( \'elementor/widget/posts/skins_init\', function( $widget ) {
  
    class cards_multi_badge_skin extends \\ElementorPro\\Modules\\Posts\\Skins\\Skin_Cards {
        protected function render_badge() {
            $taxonomy = $this->get_instance_value( \'badge_taxonomy\' );
            if ( empty( $taxonomy ) ) {
                return;
            }
            $terms = get_the_terms( get_the_ID(), $taxonomy );
            if ( ! is_array( $terms ) ) {
                return;
            }
            ?><div class="elementor-post__badges"><?php
            foreach( $terms as $term ) : ?>
                <div class="elementor-post__badge"><?php echo $term->name; ?></div>
            <?php endforeach; ?>
            </div>
            <?php
        }

        public function get_id() {
            return \'cards_multi_badge\';
        }

        public function get_title() {
            return __( \'Cards Multi Badge\', \'elementor-pro\' );
        }
    }

    // register the skin to the posts widget
    $widget->add_skin( new cards_multi_badge_skin( $widget ) );
} );
这将导致以下结果:

Post with multiple bages

现在,我也在为archive 使用以下代码在Elementor中发布小部件:

/* MULTIPLE BADGES ON ARCHIVE POST */
/* --- */
add_action( \'elementor/widget/archive-posts/skins_init\', function( $widget ) {
  
    class cards_multi_badge_skin extends \\ElementorPro\\Modules\\Archive_Posts\\Skins\\Skin_Cards {
        protected function render_badge() {
            $taxonomy = $this->get_instance_value( \'badge_taxonomy\' );
            if ( empty( $taxonomy ) ) {
                return;
            }
            $terms = get_the_terms( get_the_ID(), $taxonomy );
            if ( ! is_array( $terms ) ) {
                return;
            }
            ?><div class="elementor-post__badges"><?php
            foreach( $terms as $term ) : ?>
                <div class="elementor-post__badge"><?php echo $term->name; ?></div>
            <?php endforeach; ?>
            </div>
            <?php
        }

        public function get_id() {
            return \'cards_multi_badge\';
        }

        public function get_title() {
            return __( \'Cards Multi Badge\', \'elementor-pro\' );
        }
    }

    // register the skin to the posts widget
    $widget->add_skin( new cards_multi_badge_skin( $widget ) );
} );
但是,此代码不起作用?谁知道我做错了什么?

1 个回复
最合适的回答,由SO网友:Knight 整理而成

我也有同样的问题,只是发现你需要从扩展。\\ElementorPro\\Modules\\ThemeBuilder\\Skins\\Posts\\u Archive\\u Skin\\u Cards而不是\\ElementorPro\\Modules\\Posts\\Skins\\Skin\\u Cards