Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I hide the img-tag if there is no attacment?

(function is from this tutorial: http://wp.tutsplus.com/tutorials/automagic-post-thumbnails-image-management/)

<img src="<?php get_attachment_picture();?>" />

I need something like this:

<?php if ( get_attachment_picture()) { ?>
<img src="<?php get_attachment_picture();?>">
<?php } else { ?>
show nothing, not even av default image
<?php } ?>

Here is the function I use:

<!?php 

/* Function to process your thumbnail & image
   Copy and paste the code below to your functions.php */

function get_attachment_picture(){
    global $post, $posts;
    $related_thumbnail =  get_post_meta($post->ID, 'image', $single = true);                            //read post meta for image url

    if($related_thumbnail == ""):
        $attachments = get_children( array(
                                    'post_parent'    => get_the_ID(),
                                    'post_type'      => 'attachment',
                                    'numberposts'    => 1,
                                    'post_status'    => 'inherit',
                                    'post_mime_type' => 'image',
                                    'order'          => 'ASC',
                                    'orderby'        => 'menu_order ASC'
                                    ) );
        if(!empty($attachments)):                                               //check if there an attachment or not
            foreach ( $attachments as $attachment_id => $attachment ) {
              if(wp_get_attachment_image($attachment_id) != ""):
                  $related_thumbnail = wp_get_attachment_url( $attachment_id );
              endif;
            }
        else:                                                           // if no attachment
            $first_img = '';
            ob_start();
            ob_end_clean();
            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
            $first_img = $matches [1] [0];

            if(!empty($first_img)):
                $related_thumbnail = $first_img;
            else:
                $related_thumbnail = "images/default_thumbnail.jpg";                            //define default thumbnail, you can use full url here.
            endif;
        endif;
    endif;  

    echo $related_thumbnail;
} ?>
share|improve this question
    
Could you provide more information? That's not a standard WordPress function, and without context we cannot help. A google search suggests you're trying to follow this: wp.tutsplus.com/tutorials/… –  Tom J Nowell Apr 13 '12 at 10:42
    
Again, please post the source code for get_attachment_picture without it this question is unanswerable. –  Tom J Nowell Apr 13 '12 at 13:50

1 Answer 1

up vote 1 down vote accepted

Assuming you're following this tutorial:

http://wp.tutsplus.com/tutorials/automagic-post-thumbnails-image-management/

Replace the echo statement at the end of get_attachment_picture with a return, then change your image code.

Here is your new image code:

// get the URL of the image
$src = get_attachment_picture();

// if the url is not empty, display the image
if(!empty($src)){
    ?>
    <img src="<?php echo $src; ?>" />
    <?php
} else {
    // the url was empty, do not display the image
}

Here's the modified function:

function get_attachment_picture(){  
    global $post, $posts;  
    $related_thumbnail =  get_post_meta($post->ID, 'image', true);                          //read post meta for image url  

    if($related_thumbnail == ""):
        $attachments = get_children( array(  
                                    'post_parent'    => get_the_ID(),  
                                    'post_type'      => 'attachment',  
                                    'numberposts'    => 1,  
                                    'post_status'    => 'inherit',  
                                    'post_mime_type' => 'image',  
                                    'order'          => 'ASC',  
                                    'orderby'        => 'menu_order ASC'  
                                    ) );  
        if(!empty($attachments)):                                               //check if there an attachment or not  
            foreach ( $attachments as $attachment_id => $attachment ) {  
              if(wp_get_attachment_image($attachment_id) != ""):  
                  $related_thumbnail = wp_get_attachment_url( $attachment_id );  
              endif;  
            }  
        else:                                                           // if no attachment  
            $first_img = '';  

            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);  
            $first_img = $matches [1] [0];  

            if(!empty($first_img)):  
                $related_thumbnail = $first_img;  
            else:  
                $related_thumbnail = "";
            endif; 
        endif; 
    endif;   

    return $related_thumbnail; 
} 
share|improve this answer
    
hi Tom, that didnt work. 1. you got an error in one line doubling '!emptyempty', but changed that and still didnt work –  Demilio Apr 13 '12 at 11:36
    
I'm looking for something like this <?php if ( get_attachment_picture()) { ?><img src="<?php get_attachment_picture();?>"><?php } else { ?>show nothing, not even av default image<?php } ?> –  Demilio Apr 13 '12 at 11:42
    
$post--->ID won't work ;) –  kaiser Apr 13 '12 at 12:02
    
tried without that to, still wont work. The function is fine and works well, but I need an if/else conditional code that can show the image if there is an attachement and then dont show anything if not. The code I have now shows a default image if no attachement image –  Demilio Apr 13 '12 at 12:29
1  
sorry. It works now!!!! Thanks :) –  Demilio Apr 13 '12 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.