Jos*_*eau 3 php syntax wordpress
我有一个wordpress安装问题.当我在本地工作时,此功能正常工作:
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;
}
Run Code Online (Sandbox Code Playgroud)
这将检索安装在介质上的所有映像,并将它们返回到数组中.
但是,当我将我的安装放在远程站点上时,当我尝试使用包含此功能的主题时,我收到此错误:
Parse error: syntax error, unexpected '[' in /hermes/bosoraweb133/b2623/ipg.yourdomaincom/98EMcBee/wp-content/themes/98emcbee/functions.php on line 52
Run Code Online (Sandbox Code Playgroud)
第52行是foreach中的这一行:
array_push($images, wp_get_attachment_image_src($post->ID, 'full')[0]);
Run Code Online (Sandbox Code Playgroud)
为什么我会在远程站点而不是本地站点上收到此错误?
那是因为你正在使用只在PHP 5.4及更高版本中提供的数组解除引用.您使用的是PHP 5.3或更早版本.
$array = wp_get_attachment_image_src($post->ID, 'full');
array_push($images, $array[0]);
Run Code Online (Sandbox Code Playgroud)