0

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
julesverne
  • 109
  • 5

2 Answers2

2

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;
rid
  • 61,078
  • 31
  • 152
  • 193
  • 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 06 '14 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 06 '14 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 06 '14 at 22:16
0

This should fix your problem,

<?php $category = get_the_category($post->ID);
      $category = $category[0]->name; ?>
MrHunter
  • 1,892
  • 1
  • 15
  • 23