-2

I have a WordPress sidebar on my site that works fine on an old version of PHP, but now on production, the page will not load. This is caused by incorrect formatting of the line below, which is flagging an error due to Unparenthesized a ? b : c ? d : e not being supported.

This is the line:

$taxonomy = get_the_terms($post->ID, is_singular('post') ? 'category' : is_singular('blog') ? 'category' : is_singular('event') ? 'category' : 'vacancy_category');

I have tried multiple formats of the parenthesis to no avail. Any advice would be greatly received.

  • This structure was deprecated in PHP 7.4, and disallowed in PHP8+ because it is ambiguous. PHP's treatment of this sort of expression is at odds with most (all?) other programming languages. Things were tightened up to reduce errors. All you need to do is add appropriate parentheses. See https://wiki.php.net/rfc/ternary_associativity – Tangentially Perpendicular Jun 29 '22 at 21:00

1 Answers1

2

You can try:

$taxonomy = get_the_terms($post->ID, (is_singular('post') ? 'category' : (is_singular('blog') ? 'category' : (is_singular('event') ? 'category' : 'vacancy_category'))))

However even though this doesn't ocupy too much space, I feel like it's hard to read. You could use an if else instead to make it more readable.

  • Thank you for your response, it is now throwing the following error: PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given It's frustrating as all was fine until the update to the latest PHP today – westwickycoder Jun 29 '22 at 20:51
  • The Answer from Sorin actually worked, I had a similar piece of code to change but his answer was good! Thank you – westwickycoder Jun 29 '22 at 21:11