If has next listed, else get first listed

This seems like it should be straight forward to me, but I can’t get my head around the syntax. I want to achieve something like this (knowing it’s obviously incorrect syntax):

<?php if ($page->hasNextListed()) ? ($project = $page->nextListed()) : ($project = $page->siblings()->first()) ?>
...
<?php endif ?>

Thanks for any help!
Jamie

You can use either an if statement or the ternary operator, not mix both:

$project = $page->hasNextListed() ? $page->nextListed() : $page->siblings()->first();
dump($project); 
if($project) {
  // do something with project
}

With if statement:

$project = false;
if($page->hasNextListed()) {
  $project = $page->nextListed();
} else {
   $project = $page->siblings()->first();
}

if($project) {
  // do something
}
1 Like