skip to Main Content

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" />

2

Answers


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

    Login or Signup to reply.
  2. 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
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search