Wie blende Bild-URL wenn keine Anlage?

Wie kann ich das Img-Tag ausblenden, wenn kein Attachment vorhanden ist?

(function stammt aus diesem Tutorial: http://wp.utsplus.com/tutorials/automagic-post-thumbnails-image-management/ )

<img src="https://wordpress.stackexchange.com/questions/48896/how-to-hide-image-url-if-no-attachment/" /> 

Ich brauche so etwas:

  <img src="https://wordpress.stackexchange.com/questions/48896/how-to-hide-image-url-if-no-attachment/">  show nothing, not even av default image  

Hier ist die function, die ich verwende:

 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('//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; } ?> 

Solutions Collecting From Web of "Wie blende Bild-URL wenn keine Anlage?"

Angenommen, Sie folgen diesem Tutorial:

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

Ersetzen Sie die Echo-statement am Ende von get_attachment_picture durch eine Rückgabe und ändern Sie dann Ihren Bildcode.

Hier ist dein neuer Bildcode:

 // get the URL of the image $src = get_attachment_picture(); // if the url is not empty, display the image if(!empty($src)){ ?>  < ?php } else { // the url was empty, do not display the image } 

Hier ist die modifizierte 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('//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; }