WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to echo get_the_category($id)[0]->slug and this works on localhost but not on the server.

The error is

Parse error: syntax error, unexpected '['....

Is there a workaround?

share|improve this question

closed as off-topic by toscho Jul 26 '14 at 13:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3  
check php versions of local and live server. – Rajeev Vyas Jul 21 '14 at 14:52
    
You should do 1 thing per line, whereas you're actually doing 4 things on that line, making it difficult to debug – Tom J Nowell Jul 21 '14 at 15:17
up vote 4 down vote accepted

Probably your hosting server has a different and older version of php so this syntax is not supported.

The above syntax is implemented in PHP 5.4.

You have to do it like this:

$category = get_the_category($id);
echo $category[0]->slug;

Source:

https://bugs.php.net/bug.php?id=45906

share|improve this answer

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