如何将php命名空间与customizeregister一起使用


how use php namespace with customize_register?

我试图在wordpress中注册自定义程序函数时使用PHP命名空间,我的文件名是functions.PHP中包含的"customizer.PHP"。我通过customizer上传徽标,它给出以下错误

"致命错误:在Customizer.php中找不到类"Roots''Sage''Customizer''WP_CustomizeUpload_Control"

这是我正在使用的代码。

namespace Roots'Sage'Customizer;
add_action('customize_register',__NAMESPACE__.'''sageCustomizeRegister');
function sageCustomizeRegister( $wp_customize ) {
$wp_customize->add_setting( 'site_logo', [
        'type' => 'theme_mod',
        'default' => NULL,
] );
$wp_customize->add_control( 
    new WP_Customize_Upload_Control( 
    $wp_customize, 
    'site_logo', 
    [
            'label'      => __( 'Site Logo', 'sage' ),
            'section'    => 'title_tagline',
            'description' => 'Please upload site logo to show in header'
    ] ) 
);    
}

请指导我如何解决这个问题。

您遇到的问题是这一行:

new WP_Customize_Upload_Control( 

文件顶部的命名空间意味着您正在实例化一个不存在的类。它相当于:

new 'Roots'Sage'Customizer'WP_Customize_Upload_Control(

您需要引用全局空间中的类。只需在调用的类前面加一个反斜杠。

new 'WP_Customize_Upload_Control(

进一步阅读:http://php.net/manual/en/language.namespaces.php

相关文章:
  • 没有找到相关文章