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

Google Security at Stake- Android Apps

Android surely is doing great in the app market but still, perfectionism hasn’t been noticed. …

3 hours ago

SEO lowdown and making it work to your advantage.

The Internet offers users a wealth of information on virtually every topic imaginable, all within…

15 hours ago

Blogging is More Than Words- Top 5 Tips

The blog is a vogue for the new generation. Bantam leisure that gets you money…

1 day ago

Strategies for using social media effectively for your eCommerce business

Social media has become an essential element in today’s eCommerce marketplace. It has a significant…

2 days ago

5 Pointers To Encourage Inbound Links

It is important to draw people’s attention to your website or Blog. It isn’t easy…

2 days ago

5 Tips to Make Your Marketing Strategies Work

There are a lot of marketing strategies - some work brilliantly, some do not work…

3 days ago