Connect with us

Articles

Echo Images inside a Post into the Loop

Published

on

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..

Continue Reading
7 Comments

7 Comments

  1. Austin

    December 8, 2008 at 3:54 am

    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; ?>

  2. frosty

    December 8, 2008 at 10:59 am

    Thanks for that one!

  3. Picsbuzz

    January 7, 2009 at 9:57 pm

    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?

    • frosty

      January 7, 2009 at 10:05 pm

      Your welcome, I am not sure. If we could find out details to their database it may be possible.

  4. Nick

    January 15, 2009 at 12:38 pm

    That can be useful

    Nick’s last blog post..Montana Library

  5. Thomas

    August 10, 2009 at 11:00 am

    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

  6. Maor

    September 5, 2009 at 5:33 pm

    Thanks for the tip, liked it!
    .-= Maor´s last blog ..‫וורדפרס 2.8+: איך לא להציג קטגוריות מסוימות בתפריט הצדדי?‬ =-.

You must be logged in to post a comment Login

Leave a Reply

Premium

Revolution Two: Chrome Theme

Published

on

Benefits include the Chrome theme, unlimited theme support answered by our experts, customization techniques with our detailed theme tutorials and professional design services available by our list of recommended designers.

Revolution 2: Chrome Theme

Price:

Single License: $59.95 Buy Now!
Pro Plus All-Theme Package: $199.95 Buy Now!

Continue Reading

Premium

Revolution Two: Black Canvas

Published

on

Benefits include the Black Canvas theme, unlimited theme support answered by our experts, customization techniques with our detailed theme tutorials and professional design services available by our list of recommended designers.

Revolution 2: Black Canvas Theme

Price:

Single License: $59.95 Buy Now!
Pro Plus All-Theme Package: $199.95 Buy Now!

Continue Reading

News

Revolution Two: Agent Theme

Published

on

Benefits include the Agent theme, unlimited theme support answered by our experts, customization techniques with our detailed theme tutorials and professional design services available by our list of recommended designers.

Revolution 2: Album Theme

Price:

Single License: $59.95 Buy Now!
Pro Plus All-Theme Package: $199.95 Buy Now!

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Polo G Net Worth: 5 Interesting Facts About The Rapper

Polo G Net Worth: 5 Interesting Facts About The Rapper

Taurus Bartlett, who is best known by his stage name as Polo G is a young American rapper from Chicago, Illinois. Here are some facts about Polo G: 1. He is only 20 years old Bartlett was born on January 6, 1999, in Chicago, Illinois. His parents raised him alongside three of his other siblings […]

John Legere Net Worth: How Rich is the CEO of T-Mobile?

John Legere Net Worth: How Rich is the CEO of T-Mobile?

John Legere is an American businessman best known as the chief executive officer and former president of T-Mobile US. Legere has previously worked for At&T, Dell, and Global Crossing. As of 2019, John Legere net worth is estimated to be $85 million. Legere was born on June 4, 1958, in Massachusetts. He went to St. […]

Lil Huddy Net Worth: How Rich is the Tik Tok Superstar Actually?

Lil Huddy Net Worth: How Rich is the Tik Tok Superstar Actually?

Chase Hudson, who is better known as Lil Huddy, is an American social media personality who is very popular on the musical platform called TikTok where he has amassed a massive following of over 5.2 million followers along with more than 300 million hearts. As of 2019, Lil Huddy net worth is estimated to be […]

Cynthia Frelund Net Worth: How Rich is Cynthia Frelund Actually?

Cynthia Frelund Net Worth: How Rich is Cynthia Frelund Actually?

Cynthia Frelund is Data Analyst Expert for the NFL Media Network where she focuses on breaking down all the numbers on the show Numbers Game. Moreover, from the perspective of the fans, she is a key influencer in their fantasy football predictions. As of 2019, Cynthia Frelund net worth is estimated to be $1 million. […]

Trending