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.

Recent Posts

WP Realtor

The Ultimate WordPress Real Estate theme with custom developed plugins, single and multiple agents, extensive…

37 minutes ago

WordPress 2.6.5

WordPress 2.6.5 is immediately available and fixes one security problem and three bugs. We recommend…

13 hours ago

Comment Validation

Another plugin I am using on this site: Comment Validation by Jörn Zaefferer. From Jörn's…

1 day ago

Woopra Analytics

Woopra is the world's most comprehensive, information rich, easy to use, real-time Web tracking and…

2 days ago

WordPress Beta 3 Error

If you don't know, this site is running the latest WordPress. Which is in Beta…

2 days ago

CelebrityPress Fashion

Celebrity Press is a sexy premium WordPress theme ideal for running a Gossip / Celebrity…

3 days ago