0

I am on the single.php file and I am trying to get the category of a blog post which is either Blog or News.

The reason I want to do this is that the website I am building has both a Blog and a News page and I am using the 'Posts' to generate the pages for both. What I want to do is display the 'Blog' or 'News' text in the breadcrumbs.

This is actually working on localhost by doing the following:-

<?php $catergory = get_the_category()->name; # $catergory = get_the_category()[0]->name;
echo $catergory; ?>

But when I view it on the single page when I upload it to the server I get the following error:-

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /var/www/vhosts/m360.co.uk/irongate.m360.co.uk/wp-content/themes/irongate/single.php on line 23

Which is basically the first line from the example above.

Any ideas why this is happening?

0

Just realised what the problem is, the code should basically be:

<?php $catergory = get_the_category(); ?>
<?php echo $catergory[0]->name; ?>

I think the different versions of PHP from localhost / server were different which is why it was working on one but not the other (feel free to correct me if I'm wrong)

  • You should really upgrade your PHP version. You cannot dereference like in OP in dinosaur PHP versions, thus the syntax error. PHP 5.5 has just reached EOL'ed, so this is something to think about, specially when coming to security – Pieter Goosen Jul 14 '16 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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