I have a custom function in functions.php
that returns HTMl and some data. I need to pass some of it through a conditional to check for it's existence otherwise don't print it.
What I have looks like this:
$webinarDate = get_field('webinar_date');
// more code between here
$filteredresourcesHtml .= '
<div class="cell small-12 medium-4 large-3 equal-height">
<a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
<i class="icon-' . $post_type . '"></i>
<p class="content-type">' . ucfirst($post_type_display) . '</p>
<h4>' . $shortTitle . '</h4>
<h5>' . ($webinarDate ? date("F d, Y", strtotime($webinarDate))) . '</h5>
</a>
</div>';
The important line is this:
<h5>' . ($webinar ? date("F d, Y", strtotime($webinarDate))) . '</h5>
The line above gives me:
Fatal error: Uncaught Error: syntax error, unexpected ')'
if statement in the middle of concatenation?
PHP if... else alternative syntax throwing "unexpected ':' in..." error message
How to concatenate if statement inside echo in php?
All of these are saying to use a ternary operator, which I am doing however it's not clear how specifically to format this, and the answers above don't provide clarity on how to leverage with the strtotime
function and outputting a date.
Concatenation of multiple ternary operator in PHP? unexpected ')'
This suggests you need an else so I tried:
<h5>' . ($webinarDate ? (date("F d, Y", strtotime($webinarDate)) : ('')) . '</h5>
But this gives me
Fatal error: Uncaught Error: syntax error, unexpected ':'
How do you use a ternary operator in string concatenation with strtotime? What specifcally is the syntax for this?
The closest I currently have it is:
<h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>
But on the whole statement:
$filteredresourcesHtml .= '
<div class="cell small-12 medium-4 large-3 equal-height">
<a class="asset-block ' . $post_type . '" href="' . $resource_url . '">
<i class="icon-' . $post_type . '"></i>
<p class="content-type">' . ucfirst($post_type_display) . '</p>
<h4>' . $shortTitle . '</h4>
<h5>' . (($webinarDate) ? (date("F d, Y", strtotime($webinarDate))) . '</h5>
</a>
</div>';
I get an error on the last div that says
Fatal error: Uncaught Error: syntax error, unexpected ';'
' . ($webinar ? date("F d, Y", strtotime($webinarDate)) : "" ) . '
` . Note the number of opening and closing brackets and compare with what you tried. – Alain Doe Jun 01 '21 at 20:44