如何向定制器WordPress的自定义控件添加新参数?

时间:2016-06-24 作者:Sinac

问题是新参数不会显示在customizer中。

首先,我们extend WP_Customize_Control 对于此类:

class WP_Customize_Foo_Control extends WP_Customize_Control {
    public $type = \'foo\';

    public function render_content() {
        echo $this->label;
        echo $this->description;

        echo $this->foo;  // <--- problem is here, it\'s not display
        echo $this->moo;  // <--- problem is here, it\'s not display
    }
}
在下一步中,我们使用add_settingadd_control 在中显示新控件static_front_page 部分

$wp_customize->add_setting( \'foo_one\', array(
    \'type\' => \'theme_mod\',
    ));

$wp_customize->add_control( new WP_Customize_Foo_Control(
    $wp_customize,
    \'foo_one\',
    array(
        \'label\'         => \'Label\',
        \'description\'   => \'desc\',
        \'type\'          => \'foo\',
        \'section\'       => \'static_front_page\',

        \'foo\'           => \'John Doe\',
        \'moo\'           => array(\'some\',\'array\',\'goes\',\'here\')

        ) 
    ));
问题是,foomoo 参数未显示在自定义程序中。

1 个回复
SO网友:TheDeadMedic

如果您检查WP_Customize_Control::__construct:

public function __construct( $manager, $id, $args = array() ) {
    $keys = array_keys( get_object_vars( $this ) );
    foreach ( $keys as $key ) {
        if ( isset( $args[ $key ] ) ) {
            $this->$key = $args[ $key ];
        }
    }

    // [redacted]
 }
因此,您可以看到,为了让它获取自定义参数,需要将它们声明为自定义类的属性:

class WP_Customize_Foo_Control extends WP_Customize_Control {
    public $foo;
    public $moo;
}

相关推荐