【问题标题】:Fatal error when adding setting to customizer in wordpress在 wordpress 中向定制器添加设置时出现致命错误
【发布时间】:2014-07-10 16:28:16
【问题描述】:

我正在尝试在 Wordpress 定制器中添加一个额外的设置。您可以在网站标题和标语部分添加额外的设置,还是必须创建自己的部分?这是我的 functions.php 文件中的代码:

function mytheme_customize_register( $wp_customize )
{
    $wp_customize->add_control(
        new WP_Customize_Control(
            $wp_customize,
            'your_setting_id',
            array(
                'label'          => __( 'Your Label Name', 'theme_name' ),
                'section'        => 'title_tagline',
                'settings'       => 'your_setting_id',
                'type'           => 'text'
            )
        )
    );
}
add_action( 'customize_register', 'mytheme_customize_register' );

如果我尝试转到管理区域中的自定义主题页面,但我收到以下错误:

Fatal error: Call to a member function check_capabilities() on a non-object in C:\xampp\htdocs\one-gas\wp-includes\class-wp-customize-control.php on line 161

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    是的,您可以添加到标题和标语部分。尝试使用此代码:

    $wp_customize->add_control(
        new WP_Customize_Control(
            $wp_customize,
            'your_setting_id',
            array(
                'label'          => __( 'Your Label Name', 'theme_name' ),
                'section'        => 'title_tagline',
                'settings'       => 'your_setting_id',
                'type'           => 'text'
                )
            )
        )
    );
    

    在 WordPress Codex 上阅读有关 Theme Customization API 的更多信息。

    【讨论】:

    • 嗨,首先感谢您的帮助。我已经尝试过您的代码,但出现错误(请参阅更新的问题)
    • 您需要更改通用信息,例如setting_id 和theme_name。这就是它不起作用的原因。
    【解决方案2】:

    我最终修复了它,因为我必须先定义一个设置,所以这是工作代码:

    function mytheme_customize_register( $wp_customize )
    {
        $wp_customize->add_setting('subtagline', array(
            'default' => 0,
            'type' => 'option'
        ));
    
        $wp_customize->add_control(
            new WP_Customize_Control(
                $wp_customize,
                'subtagline',
                array(
                    'label'          => __( 'Sub Tagline', 'One Gas' ),
                    'section'        => 'title_tagline',
                    'settings'       => 'subtagline',
                    'type'           => 'text'
                )
            )
        );
    }
    add_action( 'customize_register', 'mytheme_customize_register' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-05
      • 2012-01-27
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      相关资源
      最近更新 更多