解析错误:语法错误,第 169 行中出现意外的“[" [英] Parse error: syntax error, unexpected '[' in line 169

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

问题描述

我一直在为我的 worpress 站点离线处理此代码错误.

<块引用>

解析错误:语法错误,C:\Users\guyzer\Desktop\InstantWP_4.3\iwpserver\htdocs\wordpress\wp-content\themes\thesis_182\custom\custom_functions.php 中第 169 行出现意外的["

行代码错误第 169 行: $post_date = explode(" ", $post_event)[0];

在实时站点上,站点正在运行,但是当我使用复制器复制实时站点并传输到离线或其他服务器时,总是会发生此错误,从而导致站点关闭.希望你能帮我解决这个错误!

以下是错误的完整代码:

 

<div class="tab-sidebars"><h1><a href="<?php echo get_site_url()?>/gigs">GIGS</a></h1><h3><a href="<?php echo get_site_url()?>/gigs/today">&#8226;今天</a></h3><h3><a href="<?php echo get_site_url()?>/gigs/weeks">&#8226;周<h3><a href="<?php echo get_site_url()?>/gigs/month">&#8226;月

<div id="gigs-carousel" class="post-container"><a class="buttons prev" href="#"></a><div class="viewport"><ul class="overview" style="width:9999px !important"><?php全球 $post;$today = getdate();$args = array( 'category_name' => 'gigs','post_type' =>'邮政','meta_key' =>custom_event_date,'orderby' =>元值,'订单' =>'ASC','showposts' =>'99');$the_query = new WP_Query($args);while($the_query->have_posts()) :$the_query->the_post();$post_id = $post->ID;$post_event = get_post_meta($post_id, 'custom_event_date', true);$post_date =explode(" ", $post_event)[0];$post_time =explode(" ", $post_event)[1];$post_day =explode("-", $post_date)[2];$post_permalink = get_permalink($post_id);$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail');$date_now = date("Y-m-d H:i");$date_compare = $post_event;$date_result = strtotime($date_compare) - strtotime($date_now);$current_month = date("m");$event_month = expand("-", $post_date)[1];if($date_result > 0 && $current_month == $event_month) :?><li style="background-image:url(<?php echo $thumbnail_src[0]?>)"><div class='post-day'><?php echo $post_day?></div><a class='post-item' href="<?php echo $post_permalink?>"><div class='post-title'><?php echo get_the_title();?></div><div class='post-sub'><?php echo convert_time($post_time)?></div><div class='post-excerpt'><?php the_excerpt();?></div></a><?php万一;终了;wp_reset_postdata();?></div><!-- .viewport --><a class="下一个按钮" href="#"></a></div><!-- #gigs-carousel -->

解决方案

$post_date = explode(" ", $post_event)[0];

您可能正在尝试在不支持它的 PHP 版本上使用数组取消引用功能.它仅适用于 PHP 5.4+ 版本.

来自 PHP 手册:

<块引用>

从 PHP 5.4 开始,可以直接对函数或方法调用的结果进行数组解引用.之前只能使用临时变量.

正如它所说,您必须在旧版本的 PHP 上使用临时变量:

$temp = expand(" ", $post_event);$post_date = $temp[0];

类似地改变所有的出现.

或者,您可以使用 list() 在一行中完成(不过会降低可读性):

即可以替换:

$post_date =explode(" ", $post_event)[0];$post_time =explode(" ", $post_event)[1];

这样:

list($post_date, $post_time) = expand(" ", $post_event);

但是,使用临时变量并手动分配值会更简洁易读.

I've been working on this code error for my worpress site offline.

Parse error: syntax error, unexpected '[' in C:\Users\guyzer\Desktop\InstantWP_4.3\iwpserver\htdocs\wordpress\wp-content\themes\thesis_182\custom\custom_functions.php on line 169

Line code error line 169: $post_date = explode(" ", $post_event)[0];

On the live site, site is working but when i duplicate the live site using duplicator and transfer to offline or other servers this error always occurs which turn the site down. I hope you can help me solve this error!

Here is the whole code for the error:

    <div class="page">

        <div class="tab-sidebars">
            <h1><a href="<?php echo get_site_url()?>/gigs">GIGS</a></h1>
            <h3><a href="<?php echo get_site_url()?>/gigs/today">&#8226; Today</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/weeks">&#8226; Weeks</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/month">&#8226; Month</a></h3>
        </div>

        <div id="gigs-carousel" class="post-container">

            <a class="buttons prev" href="#"></a>

            <div class="viewport">
                <ul class="overview" style="width:9999px !important">
                <?php
                global $post;
                $today = getdate();
                $args = array(  'category_name' => 'gigs', 
                                'post_type' => 'post', 
                                'meta_key' => custom_event_date, 
                                'orderby' => meta_value, 
                                'order' => 'ASC', 
                                'showposts' => '99'
                            );

                $the_query = new WP_Query($args);
                while($the_query->have_posts()) : 
                    $the_query->the_post();
                    $post_id = $post->ID;
                    $post_event = get_post_meta($post_id, 'custom_event_date', true);
                    $post_date = explode(" ", $post_event)[0];
                    $post_time = explode(" ", $post_event)[1];
                    $post_day = explode("-", $post_date)[2];
                    $post_permalink = get_permalink($post_id);
                    $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail' );

                    $date_now = date("Y-m-d H:i");
                    $date_compare = $post_event;
                    $date_result =  strtotime($date_compare) - strtotime($date_now);

                    $current_month = date("m");
                    $event_month = explode("-", $post_date)[1];

                    if($date_result > 0 && $current_month == $event_month) :
                ?>
                    <li style="background-image:url(<?php echo $thumbnail_src[0]?>)">
                        <div class='post-day'><?php echo $post_day?></div>
                        <a class='post-item' href="<?php echo $post_permalink?>" >
                            <div class='post-title'><?php echo get_the_title();?></div>
                            <div class='post-sub'><?php echo convert_time($post_time)?></div>
                            <div class='post-excerpt'><?php the_excerpt(); ?></div>
                        </a>
                    </li>

                <?php
                    endif;
                endwhile;
                wp_reset_postdata();
                ?>
                </ul>
            </div><!-- .viewport -->


            <a class="buttons next" href="#"></a>

        </div><!-- #gigs-carousel -->

    </div>

解决方案

$post_date = explode(" ", $post_event)[0];

You're probably trying to use array dereferencing feature on a PHP version that doesn't support it. It's available only on PHP 5.4+ versions.

From the PHP manual:

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

As it says, you'll have to use a temporary variable on older versions of PHP:

$temp = explode(" ", $post_event);
$post_date = $temp[0];

Change all the occurences similarly.

Or, you could use list() to do it in one line (reduces readability a bit, though):

That is, you can replace:

$post_date = explode(" ", $post_event)[0];
$post_time = explode(" ", $post_event)[1];

with this:

list($post_date, $post_time) = explode(" ", $post_event);

However, using a temporary variable and manually assigning the values is more neater and readable.

这篇关于解析错误:语法错误,第 169 行中出现意外的“["的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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