Wordpress 语法错误,意外 [

编程入门 行业动态 更新时间:2024-10-25 14:29:48
本文介绍了Wordpress 语法错误,意外 [的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我在安装 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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 13:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1410193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:意外   语法错误   Wordpress

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!