WordPress.org

Ready to get started?Download WordPress

Forums

echo wp_get_attachment_image (6 posts)

  1. mrfraz
    Member
    Posted 1 year ago #

    foreach ($files as $att) {
     echo wp_get_attachment_image($att); }

    Need a little help.. for if empty..
    echo.. nothing.

    Thanks!

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. mrfraz
    Member
    Posted 1 year ago #

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

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

  5. mrfraz
    Member
    Posted 1 year ago #

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

  6. bcworkz
    Member
    Posted 1 year ago #

    You dropped a line :)

    foreach ($files as $att) {
       $img = wp_get_attachment_image($att);
       if('' == $img) {

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.