需要帮助未定义的偏移错误


Need Help Undefined Offset Error

想知道是否有人能帮我。我一直在到处寻找答案,但似乎找不到答案。

我是一个PHP新手,对WordPress非常熟悉。我在默认的二十四岁主题中添加了三个新布局。我设想的工作方式是,如果有人选择了一个特定的布局,就会出现一个额外的侧边栏。所以我想出了以下方法(是的,我知道这可能不正确,但我自己做了,它有效…哈哈)

            $options = themeawesome_get_theme_options();
            $classes = $options['three-column' || 'three-column-left' || 'three-column-right'];
            if ( 'content' != $classes ) {
            get_sidebar('alt');
            }

like说它工作得很好,如果在主题选项面板中选择了这些选项中的任何一个,就会显示alt侧边栏。

唯一的问题是我得到以下错误:

未定义偏移:第8行1

第8行是上面的第2行代码。

有人能帮我删除这个错误吗。非常感谢您的帮助,并提前向您表示感谢。

您不能在一个数组中使用多个索引,因为您正试图使用:

$classes = $options['three-column' || 'three-column-left' || 'three-column-right'];

我不太确定你想达到什么目标,所以我有很多关于如何解决它的答案。

首先,声明可用主题的列表:

$themes = array('three-column', 'three-column-left', 'three-column-right');

如果您希望每个类都有一个数组$classes,请尝试:

$classes = array();
foreach ($themes as $theme) {
    if (isset($options[$theme])) {
        $classes = $options[$theme]; 
    }
}

如果你想获得"第一个可用的主题",请尝试:

$classes = '';
foreach ($themes as $theme) {
    if (isset($options[$theme])) {
        $classes = $options[$theme];
        break;
    }
}

因为您正在遵循if语句(if ('content' != $classes))检查单个字符串,所以我的第二个示例可能适合您的需要。