5

我正在构建自己的 wordperss 主题,当开始使用 WordPress Customizer 的主题选项时,我遇到了一些麻烦。

基本上我试图创建一个文本区域,我读过的内容我需要创建一个扩展类,然后在 WordPress 的 add_control 函数下调用它。

我已经尝试过了,并且在定制器模式下一切正常,但是一旦我进入网站的任何其他部分,我就会收到此错误:

致命错误:找不到类“WP_Customize_Control”

正如我所说,它在定制器中 100% 工作,它是自我的,但包括管理员在内的任何其他页面我都会收到此消息。

这是课程:

class ublxlportfolio_textarea extends WP_Customize_Control {
    public $type = 'textarea';

    public function render_content() {
        ?>
        <label>
        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
        <textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
        </label>
        <?php
    }
}

我需要将其包装在条件标签中吗?如果是这样那会是什么?

我做这一切都错了吗?

4

4 回答 4

20

澄清@Robert的正确答案:

仅当实际使用主题定制器时才加载类 WP_Customize_Control。因此,您需要在绑定到“customize_register”操作的函数中定义您的类。

例子:

add_action( 'customize_register', 'my_customize_register' );

function my_customize_register($wp_customize) {

  //class definition must be within my_customie_register function
  class ublxlportfolio_textarea extends WP_Customize_Control { ... }

  //other stuff
}
于 2014-07-29T09:40:38.687 回答
5

在类定义之前需要以下行:

include_once ABSPATH . 'wp-includes/class-wp-customize-control.php';

我有同样的问题并从谷歌登陆这里,希望这对某人有帮助!

于 2018-01-21T15:57:40.143 回答
1

发现该类需要进入注册函数!

于 2013-05-13T22:53:31.683 回答
0

提醒:如果您只是在扩展时忘记检查 WP_Customize_Control 类是否存在。如果您在未使用主题定制器的页面中,此提醒可能会帮助您调试此问题;由于仅在实际使用主题定制器时才加载 WP_Customize_Control 类。

if (class_exists('WP_Customize_Control')) {
    class yourCustomControlClass extends WP_Customize_Control {
       // control actions
    }
}

干杯!

于 2020-07-24T12:43:39.837 回答