Title: Need Simple PHP Code
Last modified: August 19, 2016

---

# Need Simple PHP Code

 *  [gsweb](https://wordpress.org/support/users/gsweb/)
 * (@gsweb)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/)
 * Right now I use WP Post Thumbnail (plugin) and the following code to pull up 
   images:
    **<img src=”<?php echo get_post_meta($post->ID, ‘pft_square’, true);?
   >” alt=”<?php the_title(); ?>” class=”img” />**
 * I need a code which will pull up not only **pft_square **but **pft_widescreen**
   and **pft_rectangle** also. Only one image will be displayed for each post, so
   it needs to also recognize when no thumbnail is given if possible.

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236140)
 * how is it supposed to work, that the first image found of the 3 possible images
   is the one displayed, checking in the order you specified?
 *  Thread Starter [gsweb](https://wordpress.org/support/users/gsweb/)
 * (@gsweb)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236280)
 * The order doesn’t matter, as long as it only displays one of the images.
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236304)
 * Try this code (didn’t test it). It reads 1st custom field. If its empty or not
   existing it reads second, if 2nd empty or not existing reads 3rd.
    When a value
   has been stored to the variable (meaning it found a custom field) it displays
   the image it found. If none found no image is displayed.
 *     ```
       <?php
       $src = get_post_meta($post->ID, 'pft_square', true);
       if (!$src) get_post_meta($post->ID, 'pft_widescreen', true);
       if (!$src) get_post_meta($post->ID, 'pft_rectangle', true);
       if ($src) { ?>
          <img src="<?php echo $src); ?>" alt="<?php the_title(); ?>" class="img" />
       <?php } ?>
       ```
   
 *  [jonimueller](https://wordpress.org/support/users/jonimueller/)
 * (@jonimueller)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236316)
 * I bet the plugin author would give you some help. Have you tried asking or dropping
   a comment on the plugin’s page? That’s a pretty nifty plugin, BTW.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236346)
 * stvwlf, slight error mate in what you post…
 * These 2 lines…
 *     ```
       if (!$src) get_post_meta($post->ID, 'pft_widescreen', true);
       if (!$src) get_post_meta($post->ID, 'pft_rectangle', true);
       ```
   
 * … i believe should be..
 *     ```
       if (!$src) $src = get_post_meta($post->ID, 'pft_widescreen', true);
       if (!$src) $src = get_post_meta($post->ID, 'pft_rectangle', true);
       ```
   
 * Ternary equivalent… (for the sake of doing the same, another way..)
 *     ```
       <?php
       $src = (get_post_meta($post->ID, 'pft_square', true)) ? get_post_meta($post->ID, 'pft_square', true) : false;
       $src = (!$src && get_post_meta($post->ID, 'pft_widescreen', true)) ? get_post_meta($post->ID, 'pft_widescreen', true) : false;
       $src = (!$src && get_post_meta($post->ID, 'pft_rectangle', true)) ? get_post_meta($post->ID, 'pft_rectangle', true) : '/path/to/default/image.jpg';
       ?>
       <img src="<?php echo $src); ?>" alt="<?php the_title(); ?>" class="img" />
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Need Simple PHP Code’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [thumbnail](https://wordpress.org/support/topic-tag/thumbnail/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 5 replies
 * 4 participants
 * Last reply from: [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/need-simple-php-code/#post-1236346)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
