wordpress Call to a member function add_section()

2019-05-10 08:36发布

Hey I'm kinda new to php and wordpress development and I'm just experimenting with how wordpress works and how the themes work. Now I have tried looking this up many times but no help. It was always something different and things that worked for those people, didnt work for me. here is the code,

    <?php

function sandor_hero_cap() {

$wp_customize->add_section('sandor_subhead_title', array(
        'title' => __('Sub-Header', 'sandor'),
));

$wp_customize->add_setting('sandor_subhead_title', array(
        'default' => 'A Virtual PLAYGROUND',
        'capability' => 'edit_theme_options',
        'sanitize_callback' => 'sanitize_text_field'
));

$wp_customize->add_control('sandor_subhead_title', array(
        'label' => __('Sub-Header Title', 'sandor'),
        'section' => 'sandor_subhead_section',
        'priority' => 5,
        'settings' => 'sandor_subhead_title'
));

}

add_action( 'customize_register', 'sandor_hero_cap' );

?>

the error that im getting is:

Call to a member function add_section() on a non-object in functions.php line 5 I have tried a lot of things but nothing seems to be working, hopefully you guys can help me solve this issue. thanks :)

1条回答
Ridiculous、
2楼-- · 2019-05-10 09:07

You're missing the $wp_customize argument. And, if I'm not mistaken, it might be an idea to use a different name for add_setting and add_control. See the code below.

function sandor_hero_cap($wp_customize) {

    $wp_customize->add_section('sandor_subhead_title', array(
    'title' => __('Sub-Header', 'sandor'),
));

    $wp_customize->add_setting('sandor_playground', array(
    'default' => 'A Virtual PLAYGROUND',
    'capability' => 'edit_theme_options',
    'sanitize_callback' => 'sanitize_text_field'
));

$wp_customize->add_control('sandor_playground', array(
    'label' => __('Sub-Header Title', 'sandor'),
    'section' => 'sandor_subhead_section',
    'priority' => 5,
    'settings' => 'sandor_subhead_title'
));

}

add_action( 'customize_register', 'sandor_hero_cap' );
查看更多
登录 后发表回答