Categories: Articles

Echo Images inside a Post into the Loop

Here is a useful trick. Want to call or echo an image into your blog post with our using the custom fields? Well this could be tricky, but I know of a way.

All you have to do is print these lines of code into your
index.php or home.php inside the loop:

<?php
$id =$post->ID;
$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$home = get_option('home');
$pattern = '!<img.*?src="'.$home.'(.*?)"!';
preg_match_all($pattern, $the_content, $matches);
$image_src = $matches['1'][0]; ?>

then after write:

<?php if($image_src != '') { ?>
<a href="<?php echo the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image_src; ?>" alt="<?php the_title(); ?>" /></a>
<?php }  ?>

That’s all! This will look for the first image in your post and echo it in the loop, could be used to show off those pretty thumbnails for the post..

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

View Comments

  • Assuming the image has been added using the WP uploader, here's a way of doing the same thing using the WP API, which might have a performance advantage:

    <?php $attachments = (array) get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment'));
    if ( ! empty( $attachments ) ) :
    $attmt = array_shift($attachments);
    $image_thumb = wp_get_attachment_thumb_url($attmt->ID); ?>
    <a href="<?php echo the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>"><?php the_title(); ?></a><br />
    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo $image_thumb; ?>" alt="<?php the_title(); ?>" /></a>
    <?php endif; ?>

  • This is great script, it helps me a lots!!! Thank you so much!!
    I wonder if this script can grab the first image in the post which hosted on share host like: flickr or imagesharkus?

  • Hi all,
    First of all, very nice script. I noticed that the output was in html (ie, img src and thumbnailing).
    Does anyone know of any snippet to be able to echo the image via php, without having to name the actual individual image file (eg. tree.jpg), and instead using a variable from a database that names photos in a directory.?
    thanks
    tom

Recent Posts

A simple way to query posts

Here is a simple way to call query_posts with an array of options. For all…

3 hours ago

Adding a favicon to your site

Looking to add a favicon to you site? Inside your WordPress theme's functions file (functions.php)…

9 hours ago

The launch of WordPress 2.8

Wow, have you heard? WordPress has announced that the newest version, 2.8 which was thought…

21 hours ago

WPLover: On Launching a WordPress theme

I just came across this article over at WPLover. Was very good, especially since I…

1 day ago

Maintenance release for Hybrid 0.5

Today has released an update to his Hybrid Theme. Version 0.5.1 can be found at…

2 days ago

Using the swekey on your blog

If you've downloaded the Swekey plugin and plan on using it for users to login…

2 days ago