【问题标题】:Illegal string offset Warning in WordpressWordpress 中的非法字符串偏移警告
【发布时间】:2013-09-23 10:51:14
【问题描述】:

我面临错误/警告非法字符串偏移

我检查了我所有的代码,但没有找到错误原因。 使用以下功能,我的主题样式正在运行,代码是在 function.php 中以 word press 主题编写的。

( ! ) SCREAM: 忽略错误抑制 ( ! ) 警告:F:\wamp\www\wordpress-3.6.1-newsduke\wp-content\themes\hotnews\functions\theme-functions.php 中的非法字符串偏移 'face' 在第 140 行

 function freshthemes_theme_styles() {

        /* Google fonts array */
        $google_fonts = array_keys( freshthemes_typography_get_google_fonts() );

        /* Define all the options that possibly have a unique Google font */
        $body_font = ft_get_option('body_font', 'Arial, Helvetica, san-serif');
        $heading_font = ft_get_option('heading_font', 'Arial, Helvetica, san-serif');
        $menu_nav_font = ft_get_option('menu_nav_font', 'Arial, Helvetica, san-serif');

        /* Get the font face for each option and put it in an array */
        $selected_fonts = array(
            $body_font['face'],
            $heading_font['face'],
            $menu_nav_font['face'],
        );

        /* Remove any duplicates in the list */
        $selected_fonts = array_unique($selected_fonts);

        /* If it is a Google font, go ahead and call the function to enqueue it */
        foreach ( $selected_fonts as $font ) {
            if ( in_array( $font, $google_fonts ) ) {
                freshthemes_typography_enqueue_google_font($font);
            }
        }

        // Register our styles.
        wp_register_style('main', get_stylesheet_uri(), false, THEME_VERSION, 'all');
        wp_register_style('prettyPhoto', THEME_DIR . '/stylesheets/prettyPhoto.css', false, THEME_VERSION, 'all');
        wp_register_style('responsive', THEME_DIR . '/stylesheets/responsive.css', false, THEME_VERSION, 'all');
        wp_register_style('custom-style', THEME_DIR . '/functions/framework/frontend/custom-style.css', false, filemtime(THEME_PATH . '/functions/framework/frontend/custom-style.css'), 'all');

        // Enqueue them.
        wp_enqueue_style('main');
        wp_enqueue_style('custom-style');
        wp_enqueue_style('prettyPhoto');
        wp_enqueue_style('responsive');
    }

【问题讨论】:

  • 我很难相信internet and search engines 没有为Warning: Illegal string offset 列出任何内容?
  • 抱歉,没有找到任何有用的答案
  • var_dump($body_font, $heading_font, $menu_nav_font) 显示什么?
  • @anupam var_dump 显示“字符串 'Arial, Helvetica, san-serif' (length=27)” 三次。
  • @ShagunSood 请在下面查看我的回答。

标签: php wordpress


【解决方案1】:
$selected_fonts = array(
    $body_font['face'],
    $heading_font['face'],
    $menu_nav_font['face'],
);

这些变量中的一个或多个是一个字符串,您尝试像数组一样访问它,这仅在您使用比strlen-1 更猛烈的数字键访问它时才有效

要确认这一点,也请执行var_dump($body_font, $heading_font, $menu_nav_font) 检查哪个实际上不是数组,而是字符串。

【讨论】:

  • var_dump 显示“字符串 'Arial, Helvetica, san-serif' (length=27)” 三次。
  • 好吧,那么我的假设是正确的,你有一个非法的偏移量,因为你试图访问一个string,就好像它是一个array
  • 请务必给我您的电子邮件地址,以及您的任务列表,我会在明天中午之前通过电子邮件将完整的项目发送给您,先生。
【解决方案2】:

试试:

$selected_fonts = array(
    $body_font,
    $heading_font,
    $menu_nav_font,
);

由于$body_font、$heading_font 和$menu_nav_font 是字符串,使用它们作为数组会产生警告。

编辑:

更通用:

$selected_fonts = array(
    is_array($body_font) && isset($body_font['face']) ? $body_font['face'] : $body_font,
    is_array($heading_font) && isset($heading_font['face']) ? $heading_font['face'] : $heading_font,
    is_array($menu_nav_font) && isset($menu_nav_font['face']) ? $menu_nav_font['face'] : $menu_nav_font,
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    • 2021-09-30
    • 2013-09-13
    • 2013-04-11
    • 2012-04-09
    相关资源
    最近更新 更多