Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 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 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 at 15:17

1 Answer 1

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.