WordPress中在elementor中的产品分类模版中,如果有多个产品列表的模版,让子分类根据父分类自动绑定到模版里面

将以下简码插入到主题function.php文件里面

//子分类模板继承上级分类模板,放到functions.php 最下面
add_action( 'elementor/theme/register_conditions', function( $conditions_manager ) { 
    class Subcategory_Archive extends ElementorPro\Modules\ThemeBuilder\Conditions\Taxonomy {
        private $taxonomy;

        public function get_name() {
            return 'child_of_' . $this->taxonomy->name;
        }

        public function get_label() {
            return sprintf( __( 'Direct Child %s Of', 'elementor-pro' ), $this->taxonomy->labels->singular_name );
        }

        public function __construct( $data ) {
            parent::__construct( $data );

            $this->taxonomy = $data['object'];
        }

        public function is_term() {
            $taxonomy = $this->taxonomy->name;
            $current = get_queried_object();
            return ( $current && isset( $current->taxonomy ) && $taxonomy === $current->taxonomy );
        }

        public function check( $args ) {
            $id = (int) $args['id'];
            /**
             * @var \WP_Term $current
             */
            $current = get_queried_object();
            if ( ! $this->is_term() || 0 === $current->parent ) {
                return false;
            }

            while ( $current->parent > 0 ) {
                if ( $id === $current->parent ) {
                    return true;
                }
                $current = get_term_by( 'id', $current->parent, $current->taxonomy );
            }

            return $id === $current->parent;
        }
    }
    $taxonomy = get_taxonomy('product_cat');
    $conditions_manager->get_condition( 'product_archive' )->register_sub_condition( new Subcategory_Archive([ 'object' => $taxonomy ]) );
    
}, 100 );

  

posted @ 2025-04-17 09:59  还好阿卡  阅读(64)  评论(0)    收藏  举报