【问题标题】:PHP Warning: Illegal string offset in WordpressPHP 警告:Wordpress 中的非法字符串偏移
【发布时间】:2015-06-22 04:06:28
【问题描述】:

我希望有人能帮我弄清楚一些 PHP 错误可能发生的情况。他们是这样读的:

“警告:/home/awh/public_html/wp-content/themes/Avada/shortcodes.php 第 440 行中的非法字符串偏移 'type'”

“有罪线”是 440,445,448,453,我已经在下面的代码中标记了这些行(希望可以吗?)

该网站是 www.advancewithhealth.com 并且错误似乎是由于滑块 PHP 代码中的一些错误而产生的(这是一个家庭朋友,所以我不确定她可能会意外发生什么变化)。我在下面包含了有罪的行:

//////////////////////////////////////////////////////////////////
// Slide
//////////////////////////////////////////////////////////////////
add_shortcode('slide', 'shortcode_slide');
function shortcode_slide($atts, $content = null) {
    $str = '';
    if($atts['type'] == 'video') { // <-- 440
        $str .= '<li class="video">';
    } else {
        $str .= '<li class="image">';
    }
    if($atts['link']): // <-- 445
    $str .= '<a href="'.$atts['link'].'">';
    endif;
    if($atts['type'] == 'video') { // <-- 448
        $str .= $content;
    } else {
        $str .= '<img src="'.$content.'" alt="" />';
    }
    if($atts['link']): // <-- 453
    $str .= '</a>';
    endif;
    $str .= '</li>';

    return $str;
}

提前感谢您的帮助!

热情洋溢, 卡罗

【问题讨论】:

  • 您可能传递的是字符串而不是数组。
  • 调用此函数的代码似乎传递的是字符串而不是数组。你需要看看那里。
  • @MikeW 我什至没有在整个脚本中看到对shortcode_slide 的调用。我看到一个电话add_shortcode。我错过了函数调用还是 OP 的脚本不完整?
  • 谢谢大家。我看到了一些类似的线程,但由于我有限的 php 知识,它并没有真正帮助。对不起,如果它是重复的,我应该删除我的帖子吗?

标签: php wordpress


【解决方案1】:

发生这种情况是因为您没有将 type 属性传递给短代码的参数。您可以使用以下方法避免该错误:

[slide type='video'/]

也就是说,correct way to account for this programmatically and reliably 是使用shortcode_atts() 函数来设置$atts 数组的默认值。

// the default $atts values
$defaults = array( 'type' => 'default value' );

// use array merge to override values in $defaults with those in $atts
$atts = shortcode_atts( $defaults, $atts );

// now the index will be set even if it wasn't passed into the shortcode
if( $atts['type'] == 'video' ) {

如果您不想设置任何默认值(即使您可以默认为false 或其他值),您也可以在使用isset() 访问之前检查是否在数组上设置了索引。我会推荐使用上面的方法。

if( isset( $atts['type'] ) && $atts['type'] == 'video' ) { 

【讨论】:

    【解决方案2】:

    今天我遇到了同样的问题,我通过更改调用滑块的代码来解决它,类似于:

    [slider][slide link=""]image link[/slide][slide link=""]image link here[/slide][/slider]
    

    在我的情况下,link="" 丢失了。

    【讨论】:

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