• 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

    (@bcworkz)

    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

    (@mrfraz)

    <?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

    (@bcworkz)

    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

    (@mrfraz)

    <?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

    (@bcworkz)

    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.