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?

  • 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

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;



| improve this answer | |

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