如何为Elementor主题构建器的产品归档添加专属自定义条件
解决Elementor Product Archive自定义条件归属问题
错误原因
你遇到的Call to a member function register_sub_condition() on bool错误,是因为product_archive并非Elementor的顶级条件,它是archive顶级条件下的子条件,直接用$conditions_manager->get_condition('product_archive')会返回false,自然无法调用register_sub_condition方法。
解决方案
要让自定义条件仅归属到Product Archive,需要两步调整:
1. 修改自定义条件类,指定父条件
在你的My_Custom_Condition类中添加get_parent静态方法,明确该条件的父级是product_archive:
class My_Custom_Condition extends \ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base { // ... 原有代码 ... /** * Get condition parent. * * 指定该条件的父级条件 * * @since 1.0.0 * @access public * @return string */ public static function get_parent() { return 'product_archive'; } // ... 原有代码 ... }
2. 正确注册自定义条件
依然将条件注册到顶级的archive条件下,Elementor会根据get_parent的设置自动将其归到Product Archive子条件下:
$conditions_manager->get_condition( 'archive' )->register_sub_condition( new My_Custom_Condition() );
额外修复:控件选项格式错误
你的register_controls方法中,options参数格式不符合要求,应该使用键值对形式(键为存储值,值为显示文本),否则下拉选项会显示异常:
protected function register_controls() { $this->add_control( 'custom_condition', [ 'section' => 'settings', 'label' => __( 'Custom' ), 'type' => \Elementor\Controls_Manager::SELECT, 'options' => [ '1' => esc_html__( 'Option 1', 'elementor-custom-condition' ), '2' => esc_html__( 'Option 2', 'elementor-custom-condition' ), '3' => esc_html__( 'Option 3', 'elementor-custom-condition' ), ], ] ); }
验证逻辑优化
最后,确保你的check方法根据实际需求返回正确的布尔值,比如判断当前是否为Product Archive且满足自定义条件:
public function check( $args ) { // 先验证当前是Product Archive页面 if ( ! is_post_type_archive( 'product' ) ) { return false; } // 再根据自定义控件值判断逻辑 $selected_value = $this->get_control_value( 'custom_condition' ); // 示例:如果选中值为'1'则返回true return '1' === $selected_value; }
内容的提问来源于stack exchange,提问作者Lupin
相关产品推荐
相关产品推荐

