Title: echo wp_get_attachment_image
Last modified: August 21, 2016

---

# echo wp_get_attachment_image

 *  [mrfraz](https://wordpress.org/support/users/mrfraz/)
 * (@mrfraz)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/)
 *     ```
       foreach ($files as $att) {
        echo wp_get_attachment_image($att); }
       ```
   
 * Need a little help.. for if empty..
    echo.. nothing.
 * Thanks!

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

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937348)
 * Hello again!
 * I’m confused. If there is no attachment image for a particular ID, an empty string
   is returned, so you are essentially doing `echo '';`. If this is not what you
   want, then, what do you want when no image exists?
 * Also, what is `$files`? If it’s not an array of attachment IDs, your code will
   not work.
 *  Thread Starter [mrfraz](https://wordpress.org/support/users/mrfraz/)
 * (@mrfraz)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937349)
 *     ```
       <?php $meta = get_option('field_id'); if (!is_array($meta)) $meta = (array) $meta; $queried_object = get_queried_object(); $term_id = $queried_object->term_id;  $meta = isset($meta[$term_id]) ? $meta[$term_id] : array();
   
       $files = $meta['id'];
   
       if ( ! empty( $files ) ) {
       foreach ($files as $att) {
        echo wp_get_attachment_image($att); } }
       ?>
       ```
   
 * that’s my complete code now. works. show the attached image.. and nothing when
   there is no image. but i want a to show a default image, if there is no image
   attached. could you help me there? it’s code with meta taxonomy.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937386)
 * Thanks for clarifying, that is what I thought you wanted but I wanted to make
   sure.
 * Replace the current echo line with this:
 *     ```
       $img = wp_get_attachment_image($att);
          if('' == $img) {
             echo '<img src="'.content_url().'/2013/07/default.jpg">';
          } else {
             echo $img;
          }
       } }
       ```
   
 * Of course, the src path should be changed to match your actual default image,
   and you might want to add other img attributes like alt, height, width, class,
   etc.
 *  Thread Starter [mrfraz](https://wordpress.org/support/users/mrfraz/)
 * (@mrfraz)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937433)
 *     ```
       <?php $meta = get_option('spielerdaten'); if (!is_array($meta)) $meta = (array) $meta; $queried_object = get_queried_object(); $term_id = $queried_object->term_id;  $meta = isset($meta[$term_id]) ? $meta[$term_id] : array();
   
       $files = $meta['spielerbild'];
   
       if ( ! empty( $files ) ) {
       foreach ($files as $att) {
          if('' == $img) {
             echo '<img src="'.content_url().'/2013/07/default.jpg">';
          } else {
             echo $img;
          }
       } }
   
       ?>
       ```
   
 * Now he shows the replace picture… and nothing when it’s empty.
    i need default
   when it’s empty and the attached pic when.. it’s not empty?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [12 years, 10 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937438)
 * You dropped a line 🙂
 *     ```
       foreach ($files as $att) {
          $img = wp_get_attachment_image($att);
          if('' == $img) {
       ```
   

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

The topic ‘echo wp_get_attachment_image’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 5 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [12 years, 10 months ago](https://wordpress.org/support/topic/echo-wp_get_attachment_image/#post-3937438)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
