Hi I have been struggling with how to recreate a standard ACF if/else statement using Controller.
EG I need to create this with a controller:
<?php if ( get_field( 'field_name' ) ): ?>
This is displayed when the field_name is TRUE or has a value.
<?php else: // field_name returned false ?>
This is displayed when the field is FALSE, NULL or the field does not exist.
<?php endif; // end of if field_name logic ?>
I have tried the following:
From my /app/controllers/App.php:
public function pageHeaderWidth()
{
return get_field('page_header_width', 'options');
}
public function pageHeaderWidthOverride()
{
return get_field('page_header_width_override');
}
How can I repace that get_field with a variable from the controller. No matter what I try the $pageHeaderWidthOverride variable seems to have a value even though it does not output any characters or spaces. This means that I cannot use null, if_empty, or “” as ways to trigger the if statement. Any help would be greatly appreciated.
Well the first thing I notice is that you’re calling these as static functions but haven’t declared them that way.
The way Controller works, you’ll get a variable for each non-static function (the kind you’ve declared in App.php) with the same name as its function name. You also don’t need brackets for your else statement in Blade. Also also CamelCase functions are converted to snake_case by Controller. So in the example you’ve given, you’d write this instead:
That did the trick. I knew it must have been something simply. Clearly I missed the part about Controller converting camelCase to snake_case. Thank you so much. You just saved my forehead from repeated collisions with my desk.
This is also very helpful. Still wrapping my head around Blade but so far so good (aside from the utter confusion, despair and general hopelessness I have felt over the last hour or so). Loving the way my templates look with the simpler markup.
Just last week I was wondering why such a thing didn’t exist in PHP and then found out it did I just didn’t know I had to google “null coalescing operator” to find it. Silly me.