关于php:致命错误:找不到类’WP_Customize_Control’-WordPress

Fatal error: Class 'WP_Customize_Control' not found - WordPress

我正在建立自己的wordperss主题,并在WordPress Customizer上启动主题选项时遇到了一些麻烦。

基本上,我试图创建一个textarea,我已经读过我需要创建一个扩展类,然后在WordPress的add_control函数下调用它。

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

致命错误:找不到类" WP_Customize_Control"

正如我所说的,它在自定义程序中可以100%自用,但是其他任何页面(包括admin)我都会收到此消息。

这是课程:

1
2
3
4
5
6
7
8
9
10
11
12
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
    }
}

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

我做错了吗?


为了澄清@Robert的正确答案:

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

例:

1
2
3
4
5
6
7
8
9
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
}


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

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

我遇到了同样的问题,并从Google登陆了,希望对您有所帮助!


发现该类需要进入register函数内!


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

1
2
3
4
5
if (class_exists('WP_Customize_Control')) {
    class yourCustomControlClass extends WP_Customize_Control {
       // control actions
    }
}

干杯!