【问题标题】:PHP Warning Illegal string offset in WordpressPHP警告Wordpress中的非法字符串偏移
【发布时间】:2018-07-30 19:39:38
【问题描述】:

有一个警告:

警告:第 112 行 /home/procsi/galina3000.ru/wp-content/themes/memoir/epanel/custom_functions.php 中的非法字符串偏移 'alt'

警告:第 113 行 /home/procsi/galina3000.ru/wp-content/themes/memoir/epanel/custom_functions.php 中的非法字符串偏移“标题”

代码如下:

if ( function_exists('has_post_thumbnail') ) {
        if ( has_post_thumbnail( $post->ID ) ) {
            $thumb_array['use_timthumb'] = false;
            
            $args='';
            if ($class <> '') $args['class'] = $class;
            if ($alttext <> '') $args['alt'] = $alttext;
            if ($titletext <> '') $args['title'] = $titletext;
            
            $thumb_array['thumb'] = get_the_post_thumbnail( $post->ID, array($width,$height), $args );
            
            if ($fullpath) {
                $thumb_array['fullpath'] = get_the_post_thumbnail( $post->ID );
                if ($thumb_array['fullpath'] <> '') { 
                    $thumb_array['fullpath'] = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $thumb_array['fullpath'], $matches);
                    $thumb_array['fullpath'] = trim($matches[1][0]);
                }
            }
        }

如何解决?谢谢。

【问题讨论】:

  • 你能把标题也写成英文吗?
  • $args 是一个字符串。您已将其定义为带有 $args=''; 的字符串,但您将其作为带有 $args['title'] 等的数组进行访问。
  • @Variable,请不要为他人翻译帖子。他们需要自己能够做到。 meta.stackoverflow.com/questions/297673/…
  • @Don'tPanic 另一半已经是英文了,不知道不能翻译问题,会看的,谢谢(ツ)
  • @Variable 是的,在这种情况下这没什么大不了的,仅供参考。

标签: php wordpress


【解决方案1】:

它试图告诉你,你正在使用一个字符串作为数组,其中一个字符串作为键:

$args='';

if ($class <> '') $args['class'] = $class;
if ($alttext <> '') $args['alt'] = $alttext;
if ($titletext <> '') $args['title'] = $titletext;

由于您首先将 $args 声明为字符串 (''),因此当您尝试将其他参数分配给该变量时,它就是该变量的类型。

如果您改为将其声明为数组 - 因为您想将其用作数组,它应该可以按预期工作:

$args = []; // or $args = array(); if PHP version < 5.4

if ($class <> '') $args['class'] = $class;
if ($alttext <> '') $args['alt'] = $alttext;
if ($titletext <> '') $args['title'] = $titletext;

【讨论】:

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