Parse error: syntax error, unexpected ‘[‘ in line 169

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

Parse error: syntax error, unexpected ‘[‘ in C:UsersguyzerDesktopInstantWP_4.3iwpserverhtdocswordpresswp-contentthemesthesis_182customcustom_functions.php on line 169

Read More

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">• Today</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/weeks">• Weeks</a></h3>
            <h3><a href="<?php echo get_site_url()?>/gigs/month">• 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>

Related posts

Leave a Reply

4 comments

  1. $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.

  2. From the PHP docs:

    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.

    You are most likely using PHP 5.3 on the machine getting the error.

  3. I believe you need PHP >= 5.4 for that:

    explode(" ", $post_event)[0]
    

    Try:

    list($post_date, $post_time)   = explode(" ", $post_event);
    list($junk, $junk2, $post_day) = explode("-", $post_date);
    
  4. Just explode $post_event into it’s own array and then act on that.

    $post_event = get_post_meta($post_id, 'custom_event_date', true);
    $post_dates = explode(" ", $post_event); 
    $post_date = $post_dates[0];
    $post_time = $post_dates[1];
    $post_day = $post_dates[2];