Wordpress API自定义的问题

时间:2014-12-05 18:02:07

标签: php wordpress

我正在制作我的第一个Wordpress主题,我正在学习API自定义。但是这个代码我做错了。怎么了?当我加载wp-admin页面时,它显示为空白。

这是PHP代码

//Welcome Message Settings

    $wp_customize->add_setting( 'welcome_message' , array(
        'default'     => 'hi',
    ));

        $wp_customize->add_section( 'test_welcomemessage' , array(
        'title'      => __('Welcome Message','my_test'),
        'description'   => 'Write your own Welcome Message for your visitors'
    ));

    $wp_customize->add_control( 'welcome_message', array(
        'label'      => __( 'Welcome Messagee', 'mytheme' ),
        'section'    => 'test_welcomemessage',
        'settings'   => 'welcome_message'
    )));

}

这是HTML结构

<div id="welcomecontainer"> 
<div class="welcomemessage"><?php echo get_theme_mod('welcome_message'); ?></div>
</div>

此代码有什么问题? 谢谢

1 个答案:

答案 0 :(得分:0)

为了使用$wp_customize->..,您需要使用customize_register hook:

add_action( 'customize_register', 'register_my_settings' );
function register_my_settings( $wp_customize ){
  $wp_customize->add_section( 'test_welcomemessage' , array(
    'title'      => __('Welcome Message','my_test'),
    'description'   => 'Write your own Welcome Message for your visitors'
  ));
}

(我不确定订单在这里是否重要,但您需要:add_sectionadd_setting然后add_control

除此之外,在开发时,请确保将WP_DEBUG设置为true(在项目的根目录中编辑wp-config.php,并在文件的末尾找到此设置)为了查看所有错误(同时,确保服务器设置为显示错误)