WordPress主题中未定义的索引

时间:2016-11-05 10:59:02

标签: php wordpress themes wordpress-theming

我对主题开发比较陌生,我已经在这里查看了关于WordPress中“未定义索引”通知的各种帖子,但是我被卡住了。

我的themeoptions.php工作得很好,直到我从一个页面视图切换到一个标签视图。如果我在常规选项卡上保存选项,一切似乎都没问题,但是当您切换到社交选项卡时,您会收到未定义的索引通知,反之亦然。

这是通知之一:未定义索引:第228行/home/demo/public_html/wordpress/wp-content/themes/my-theme/inc/themeoptions.php中的mytheme_text_facebook < / p>

有没有人有想法?提前谢谢。

这是我的themeoptions.php:

<?php
add_action( 'admin_menu', 'mytheme_add_admin_menu' );
add_action( 'admin_init', 'mytheme_settings_init' );

function  mytheme_add_admin_menu() {
    $theme_page = add_theme_page(
        __( 'Theme Options', 'my-theme' ),   // Name of page
        __( 'Theme Options', 'my-theme' ),   // Label in menu
        'edit_theme_options',          // Capability required
        'my-theme',               // Menu slug, used to uniquely identify the page
        'mytheme_options_page' // Function that renders the options page
    );
}



function mytheme_settings_init(  ) { 
    register_setting( 'theme_options_general', 'mytheme_settings' );
    register_setting( 'theme_options_social', 'mytheme_settings' );

    //Add Sections
    add_settings_section(
        'mytheme_theme_options_general_section', 
        __( 'General settings', 'my-theme' ), 
        'mytheme_settings_general_section_callback', 
        'theme_options_general'
    );
    add_settings_section(
        'mytheme_theme_options_social_section', 
        __( 'Social settings', 'my-theme' ), 
        'mytheme_settings_social_section_callback', 
        'theme_options_social'
    );

    //Add general settings
    add_settings_field( 
        'mytheme_select_multiauthor', 
        __( 'Multi-author features', 'my-theme' ), 
        'mytheme_select_multiauthor_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
    add_settings_field( 
        'mytheme_select_navbar_image', 
        __( 'Navbar with logo', 'my-theme' ), 
        'mytheme_select_navbar_image_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
  add_settings_field( 
        'mytheme_select_navbar_searchform', 
        __( 'Navbar with search form', 'my-theme' ), 
        'mytheme_select_navbar_searchform_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
    add_settings_field( 
        'mytheme_select_sidebar_location', 
        __( 'Sidebar', 'my-theme' ), 
        'mytheme_select_sidebar_location_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
    add_settings_field( 
        'mytheme_select_scroll_down', 
        __( 'Scroll down button', 'my-theme' ), 
        'mytheme_select_scroll_down_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
    add_settings_field( 
        'mytheme_select_scroll_up', 
        __( 'Scroll up button', 'my-theme' ), 
        'mytheme_select_scroll_up_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );
    add_settings_field( 
        'mytheme_select_cdn', 
        __( 'Content delivery networks', 'my-theme' ), 
        'mytheme_select_cdn_render', 
        'theme_options_general', 
        'mytheme_theme_options_general_section' 
    );

    //Add social settings
    add_settings_field( 
        'mytheme_select_sociallinks', 
        __( 'Social links', 'my-theme' ), 
        'mytheme_select_sociallinks_render', 
        'theme_options_social', 
        'mytheme_theme_options_social_section' 
    );
    add_settings_field( 
        'mytheme_text_facebook', 
        __( 'Facebook username', 'my-theme' ), 
        'mytheme_text_facebook_render', 
        'theme_options_social', 
        'mytheme_theme_options_social_section' 
    );  
    add_settings_field( 
        'mytheme_text_twitter', 
        __( 'Twitter username', 'my-theme' ), 
        'mytheme_text_twitter_render', 
        'theme_options_social', 
        'mytheme_theme_options_social_section' 
    );
    add_settings_field( 
        'mytheme_text_linkedin', 
        __( 'LinkedIn username', 'my-theme' ), 
        'mytheme_text_linkedin_render', 
        'theme_options_social', 
        'mytheme_theme_options_social_section' 
    );
    add_settings_field( 
        'mytheme_text_xing', 
        __( 'Xing username', 'my-theme' ), 
        'mytheme_text_xing_render', 
        'theme_options_social', 
        'mytheme_theme_options_social_section' 
    );

}

function mytheme_options_page_capability( $capability ) {
    return 'edit_theme_options';
}
add_filter( 'option_page_capability_mytheme', 'mytheme_options_page_capability' );

/*------------------------------------*\
    Render general settings
\*------------------------------------*/

//Multiauthor features
function mytheme_select_multiauthor_render(  ) {
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_multiauthor]'>
        <option value='multiauthor-on' <?php selected( $options['mytheme_select_multiauthor'], 'multiauthor-on' ); ?>><?php _e('Enabled', 'my-theme'); ?></option>
        <option value='multiauthor-off' <?php selected( $options['mytheme_select_multiauthor'], 'multiauthor-off' ); ?>><?php _e('Disabled','my-theme'); ?></option>
    </select>
<?php
}

//Navbar with image
function mytheme_select_navbar_image_render(  ) {
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_navbar_image]'>
        <option value='navbarimage-on' <?php selected( $options['mytheme_select_navbar_image'], 'navbarimage-on' ); ?>><?php _e('Enabled', 'my-theme'); ?></option>
        <option value='navbarimage-off' <?php selected( $options['mytheme_select_navbar_image'], 'navbarimage-off' ); ?>><?php _e('Disabled','my-theme'); ?></option>
    </select>
<?php
}

//Navbar search form
function mytheme_select_navbar_searchform_render(  ) {
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_navbar_searchform]'>
        <option value='navbarsearch-on' <?php selected( $options['mytheme_select_navbar_searchform'], 'navbarsearch-on' ); ?>><?php _e('Enabled', 'my-theme'); ?></option>
        <option value='navbarsearch-off' <?php selected( $options['mytheme_select_navbar_searchform'], 'navbarsearch-off' ); ?>><?php _e('Disabled','my-theme'); ?></option>
    </select>
<?php
}

//Sidebar location
function mytheme_select_sidebar_location_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_sidebar_location]'>
        <option value='sidebar-left' <?php selected( $options['mytheme_select_sidebar_location'], 'sidebar-left' ); ?>><?php _e('Left','my-theme'); ?></option>
        <option value='sidebar-right' <?php selected( $options['mytheme_select_sidebar_location'], 'sidebar-right' ); ?>><?php _e('Right','my-theme'); ?></option>
        <option value='sidebar-off' <?php selected( $options['mytheme_select_sidebar_location'], 'sidebar-off' ); ?>><?php _e('Disabled','my-theme'); ?></option>
    </select>
<?php
}

//Scroll up and down buttons
function mytheme_select_scroll_down_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_scroll_down]'>
        <option value='scrolldown-on' <?php selected( $options['mytheme_select_scroll_down'], 'scrolldown-on' ); ?>><?php _e('Enabled','my-theme'); ?></option>
        <option value='scrolldown-off' <?php selected( $options['mytheme_select_scroll_down'], 'scrolldown-off' ); ?>><?php _e('Disabled', 'my-theme'); ?></option>
    </select>
<?php
}
function mytheme_select_scroll_up_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_scroll_up]'>
        <option value='scrollup-on' <?php selected( $options['mytheme_select_scroll_up'], 'scrollup-on' ); ?>><?php _e('Enabled','my-theme'); ?></option>
        <option value='scrollup-off' <?php selected( $options['mytheme_select_scroll_up'], 'scrollup-off' ); ?>><?php _e('Disabled', 'my-theme'); ?></option>
    </select>
<?php
}

//Content delivery network
function mytheme_select_cdn_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_cdn]'>
        <option value='cdn-on' <?php selected( $options['mytheme_select_cdn'], 'cdn-on' ); ?>><?php _e('Enabled','my-theme'); ?></option>
        <option value='cdn-off' <?php selected( $options['mytheme_select_cdn'], 'cdn-off' ); ?>><?php _e('Disabled', 'my-theme'); ?></option>
    </select>
<?php
}

/*------------------------------------*\
    Render social settings
\*------------------------------------*/

//Social links
function mytheme_select_sociallinks_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <select name='mytheme_settings[mytheme_select_sociallinks]'>
        <option value='sociallinks-on' <?php selected( $options['mytheme_select_sociallinks'], 'sociallinks-on' ); ?>><?php _e('Enabled', 'my-theme'); ?></option>
        <option value='sociallinks-off' <?php selected( $options['mytheme_select_sociallinks'], 'sociallinks-off' ); ?>><?php _e('Disabled','my-theme'); ?></option>
    </select>
<?php
}
//Facebook username
function mytheme_text_facebook_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <input type='text' name='mytheme_settings[mytheme_text_facebook]' value='<?php echo $options['mytheme_text_facebook']; ?>'>
    <?php
}
//Twitter username
function mytheme_text_twitter_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <input type='text' name='mytheme_settings[mytheme_text_twitter]' value='<?php echo $options['mytheme_text_twitter']; ?>'>
    <?php
}
//Linkedin username
function mytheme_text_linkedin_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <input type='text' name='mytheme_settings[mytheme_text_linkedin]' value='<?php echo $options['mytheme_text_linkedin']; ?>'>
    <?php
}
//Xing username
function mytheme_text_xing_render(  ) { 
    $options = get_option( 'mytheme_settings' );
    ?>
    <input type='text' name='mytheme_settings[mytheme_text_xing]' value='<?php echo $options['mytheme_text_xing']; ?>'>
    <?php
}

function mytheme_settings_general_section_callback(  ) { 
    echo __( 'Adapt this theme according to your needs and wishes.', 'my-theme' );
}

function mytheme_settings_social_section_callback(  ) { 
    echo __( 'A website without social media is naked. Here you can control all the social features of this theme.', 'my-theme' );
}

//Create options page
function mytheme_options_page(  ) { ?>
    <div class="wrap">
        <h1><?php _e('Theme Options','my-theme'); ?></h1>
        <?php settings_errors(); ?>
        <?php
            $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'theme_options_general';
            if( isset( $_GET[ 'tab' ] ) ) {
                $active_tab = $_GET[ 'tab' ];
            }
        ?>
        <h2 class="nav-tab-wrapper fmv-options-content">
                <a href="?page=my-theme&tab=theme_options_general" class="nav-tab <?php echo $active_tab == 'theme_options_general' ? 'nav-tab-active' : ''; ?>"><?php _e('General settings','my-theme'); ?></a>
                <a href="?page=my-theme&tab=theme_options_social" class="nav-tab <?php echo $active_tab == 'theme_options_social' ? 'nav-tab-active' : ''; ?>"><?php _e('Social settings','my-theme'); ?></a>
        </h2>
        <form action='options.php' method='post' class="fmv-options-content">
            <?php
                if( $active_tab == 'theme_options_general' ) {
                    settings_fields( 'theme_options_general' );
                    do_settings_sections( 'theme_options_general' );
                    submit_button();
        } else if( $active_tab == 'theme_options_social' ) {
                    settings_fields( 'theme_options_social' );
                    do_settings_sections( 'theme_options_social' );
                    submit_button();
        }

            ?>
        </form>
        <div class="fmv-options-sidebar">
            <p>placeholder</p>
        </div>
        <br class="clear">
    </div>
<?php } ?>

0 个答案:

没有答案