0

您好我正在尝试为我正在开发的模板创建一些自定义选项,但我似乎遇到了错误:

Warning: Illegal string offset 'show_header' in C:\xampp\htdocs\wordpress\wp-content\themes\01MyWork\includes\theme-options.php on line 62

这是似乎引发错误的行:

 $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';  

这是整个代码:

   <?php 
    function thanatos_theme_menu(){
        add_theme_page(
                       "Thanathos Theme Options", 
                       "Thanathos Theme", 
                       "administrator", 
                       "thanathos_theme_options",
                       "thanathos_theme_display_callback"
                      );
    }
    add_action('admin_menu' , 'thanatos_theme_menu');
    function thanathos_theme_display_callback(){
?>
         <div class="wrap">  
                <div id="icon-themes" class="icon32"></div>  
                <h2>Sandbox Theme Options</h2>  

                <?php settings_errors(); ?>
                <!--Create the form that will be used to render our options-->
                <form method="post" action="options.php">
                    <?php settings_fields('thanathos_theme_display_options'); ?>
                    <?php do_settings_sections( 'thanathos_theme_display_options' ); ?>             
                    <?php submit_button(); ?>
                </form>
        </div>
<?php
    }

    add_action('admin_init' , 'thanatos_initializa_theme_options');
    function thanatos_initializa_theme_options(){
        if( false == get_option( 'thanathos_theme_display_options' ) ) {    
            add_option( 'thanathos_theme_display_options' );  
        } 
        add_settings_section(
                'general_settings_section', 
                'Thanatos Options', 
                'thanatos_general_options_callback', 
                'thanathos_theme_display_options'
        );
        add_settings_field(
                'show_header',
                'Header',
                'thanathos_field_header_callback',
                'thanathos_theme_display_options',
                'general_settings_section',
                 array(                              // The array of arguments to pass to the callback. In this case, just a description.  
                    'Activate this setting to display the header.'
                 ) 
        );
        register_setting('thanathos_theme_display_options', 'thanathos_theme_display_options');
    }

    function thanatos_general_options_callback(){
        echo 'mergem la mare';
    }
    function thanathos_field_header_callback($args){
         // First, we read the options collection  
        $options = get_option('thanathos_theme_display_options');
        // Next, we update the name attribute to access this element's ID in the context of the display options array  
        // We also access the show_header element of the options collection in the call to the checked() helper function 
        $html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';  
         // Here, we'll take the first argument of the array and add it to a label next to the checkbox  
        $html .= '<label for="show_header"> '  . $args[0] . '</label>';   
        echo $html;
    }
?>
4

3 回答 3

1

是的,这是有问题的部分:

if( false == get_option( 'thanathos_theme_display_options' ) ) {    
add_option( 'thanathos_theme_display_options' );  
} 

这是thanatos_initializa_theme_options() 函数开头的初始if 语句。

您可以在http://wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-4-的非常简洁的 Theme Options API 中找到解决方案on-theme-options/#post-684925289 或者,更确切地说,更确切地说是在本文的评论部分。

我在这里粘贴史蒂夫邦迪的智能解决方案,因为出于某种原因,让页面滚动到适当的评论对我不起作用(至少在 Chrome 中)。

开始报价

(...)我遇到的一个小问题 - 我一直在重新排序代码,直到添加社交选项之前。那时我发现代码被破坏了。我会收到类似的错误

Warning: Illegal string offset 'show_header' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_content' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_footer' in ...\themes\Sandbox\functions.php

事实证明,这个错误是由于在选项表中添加了“sandbox_theme_display_options”而没有给它一个值。如果您更改 sandbox_initialize_theme_options 如下,它将创建和初始化选项,避免我和其他人遇到的错误。

function sandbox_initialize_theme_options() { 
     // If the theme options don't exist, create them. 
     if( false == get_option( 'sandbox_theme_display_options' ) ) { 
          $options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
          add_option( 'sandbox_theme_display_options', $options);
     } // end if
(...)

如果旧代码已运行,则必须首先从数据库中删除空的“sandbox_theme_display_options”值。或者,以下代码也将检测到这种情况并进行更正。

function sandbox_initialize_theme_options() { 
     // See if the options exist, and initialize them if they don't
     $options = get_option( 'sandbox_theme_display_options' );
     if( false == $options or $options == "" ) { 
          $options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
          update_option( 'sandbox_theme_display_options', $options);
     } // end if
(...)

这将检查不存在或空的选项值,并使用 update_option 而不是 add_option 初始化选项。

EOF 报价

于 2013-10-11T23:49:02.333 回答
0

您正在使用name="thanathos_theme_display_options[show_header]"纯 HTML。可能您想通过 PHP 解析字符串[show_header]

于 2012-08-22T05:45:04.400 回答
0

您可能必须运行: delete_option('thanathos_theme_display_options'); 在“admin_init”期间,如果您认为数据库中存在旧插件信息并需要重新开始。

于 2013-06-04T19:35:17.653 回答