Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

Currently, changed my localhost site to a new domain with a new url. Checked the PHP logs and got the following error?

[06-Feb-2014 19:47:51] PHP Parse error: syntax error, unexpected '[' in /var/www/... on line 128

Here is my code:

<div class="home_story_container">
<a href="<?php echo get_permalink( $post->ID ); ?>"><div class="home_preview_photo"         style="background-image:url('<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-    thumbnail' ); ?>
<?php echo $image[0]; ?>
<?php endif; ?>')">
</div></a>
<?php $category = get_the_category($post->ID)[0]->name; ?>
<?php
$category_link = get_category_link( get_cat_ID( $category ) );
?>
<a href="<?php echo esc_url( $category_link ); ?>"><span class="home_cat_tag"><?php echo     $category; ?></span></a>
<br />
<a href="<?php echo get_permalink( $post->ID ); ?>"><span class="home_title"><?php echo     get_the_title($post); ?></span></a>
<br /><br />
<span class="home_paragraph"><?php
$page_data = get_page( $post->ID );
echo strip_tags($page_data->post_excerpt);
?> – <a href="<?php echo get_permalink( $post->ID ); ?>">Read More »</a></span>
</div>

Specifically, I'm getting an error on this line:

<?php $category = get_the_category($post->ID)[0]->name; ?>

What's the problem?

share|improve this question

marked as duplicate by John Conde, andrewsi, Ian Kemp, Simon McLoughlin, M42 Feb 7 at 13:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
What version of PHP Are you running? Array dereferencing was only introduced in PHP 5.4+ –  Mark Baker Feb 6 at 21:48

2 Answers 2

up vote 1 down vote accepted

That particular syntax (function array dereferencing) has been introduced in PHP 5.4. Previous versions of PHP would mark it as a syntax error.

You could use the PHP < 5.4 syntax instead:

$categories = get_the_category($post->ID);
$category = $categories[0]->name;
share|improve this answer
    
I initially thought this could have been the problem and am confirming with the hosting servers to see what version of PHP they use. Could this cause for the entire homepage to load, but the other posts to load up fine? (Wordpress theme, blog posts are fine.) –  julesverne Feb 6 at 21:53
    
@julesverne, this should raise the syntax error as soon as the code is included. So if other pages don't include this PHP file, they should be fine. You could also use the old syntax instead. Answer updated. –  rid Feb 6 at 21:54
    
Thanks for this. I seem to have actually broken the site by making some stupid changes, so I'll have to reupload the theme and try the php change. –  julesverne Feb 6 at 22:16

This should fix your problem,

<?php $category = get_the_category($post->ID);
      $category = $category[0]->name; ?>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.