wordpress - Show attached media image if no thumbnail image detected -
i'm using
if ( has_post_thumbnail() ){}
to check if post have thumbnail image,
echo get_attached_media('image', $post->id);
display word
array
i need show attached image
if trying 1 image in post , use thumbail, might want try one:
add you're functions.php :
// url of first image in post function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; // no image found display default image instead if(empty($first_img)){ $first_img = "/images/default.jpg"; } return $first_img; }
to call put
<?php echo catch_that_image() ?>
in template file within loop.
i found brilliant code forum thread. saved me lot of times.
Comments
Post a Comment