Wordpress 语法错误,意外 [ [英] Wordpress Syntax Error, unexpected [

查看:34
本文介绍了Wordpress 语法错误,意外 [的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安装 wordpress 时遇到问题.当我在本地工作时,此功能正常工作:

I have a problem with a wordpress installation. When I work locally, this function works properly:

function get_images_from_media_library() {
  $args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => null, // any parent
    ); 
  $attachments = get_posts($args);
  $images = array();
  if ($attachments) {
    foreach ($attachments as $post) {
        setup_postdata($post);
        array_push($images, wp_get_attachment_image_src($post->ID, 'full')[0]);
    }
  }
  return $images;
}

这将检索附加到安装媒体上的所有图像,并将它们以数组形式返回.

This retrieves all the images attached to media on the installation and returns them in an array.

但是,当我将安装放在远程站点上时,当我尝试使用包含此功能的主题时会收到此错误:

However, when I put my installation up on my remote site, I get this error when I try to use the theme that holds this function:

Parse error: syntax error, unexpected '[' in /hermes/bosoraweb133/b2623/ipg.yourdomaincom/98EMcBee/wp-content/themes/98emcbee/functions.php on line 52

第 52 行是 foreach 中的这一行:

Line 52 is this line inside the foreach:

array_push($images, wp_get_attachment_image_src($post->ID, 'full')[0]);

为什么我会在远程站点而不是本地站点上收到此错误?

Why would I get this error on a remote site and not on a local site?

推荐答案

那是因为您正在使用 数组解除引用 仅在 PHP 5.4 及更新版本中可用.您使用的是 PHP 5.3 或更低版本.

That because you are using array dereferencing which is only available in PHP 5.4 and newer. You are using PHP 5.3 or older.

$array = wp_get_attachment_image_src($post->ID, 'full');
array_push($images, $array[0]);

这篇关于Wordpress 语法错误,意外 [的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆