SavPhill
SavPhill

Reputation: 655

PHP Display Image IF Else Statement

My code displays an image if the order status change matches the image name. However I have some order status' with no image and in this case there is an error where the picture cannot be found on the server.

I want to display an If Else statement where the image only shows if it matches the status name else if displays nothing.

<img src="<?php print get_template_directory_uri().'/images/truck-'.$order->status; ?>.png" />

Upvotes: 1

Views: 361

Answers (2)

Ad Fortia
Ad Fortia

Reputation: 333

You can use the php function file_exists(). Here an example snippet:

<?php
if(file_exists(get_template_directory_uri().'/images/truck-'.$order->status.'.png')) {
//display image
}
else {
//display a placeholder image
}
?>

Upvotes: 1

noah1400
noah1400

Reputation: 1509

You can check if the file exists on the server with file_exists(string $filename) Or use a try catch statement

Upvotes: 0

Related Questions