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

News

The new design in progress

Published

on

Okay, so by now you may notice something. You may be thinking “the site looks a lot different”, and you are correct. Over the past few weeks I’ve decided that the site needed a different flare and feel. While many and most site’s these days are popping up with featured posts, and jQuery sliders, I wanted to steer away from that and go old school. While I had some trouble’s activating the theme today which didn’t allow me to fully finish the theme, 99% of it is done.

The site is still build on the powerful Hybrid theme framework. Enjoy.

Continue Reading

News

Bar Camp Los Angles 7

Hello from BarCampLA in beautiful Culver City. It’s a bit early and presentation’s don’t start till after lunch. While I hadn’t planned on making a presentation, I got an email from Shen asking if I’d do a collaboration presentation, and I guess i agreed.

Published

on

So at this time Shen, myself and possibly Michael Dorausch. will be doing a panel presntation at 6:00pm in track 3 of the BarCamp room here in the OTX facility.

Links

Austin Passy
Twitter: @TheFrosty
Personal Blog: TheFrosty.com
Shen Deshayne
Twitter: @shennyg
Site: Shen Deshayne
Michael Dorausch
Twitter: @Chiropractic
Site: Michael Dorausch
Continue Reading

Child

New theme release: CULT[i]VATE

Doing theme modification and even custom template files has brought me to this point. Today I’ve created my first premium theme, and since I am a huge fan of Hybrid, I decided that a child theme would be an appropriate starting point.

Published

on

So after some time playing with many options and idea’s I’ve finally released a premium theme. This theme, which I am calling CULT[i]VATE is a child theme.

Unfamiliar with child themes? You may want to check out this post Ian Stewart wrote.

CULT[i]VATE is a child theme of the Hybrid:

Hybrid is a user-friendly, search-engine optimized theme framework, featuring 14 custom page templates and 9 widget-ready areas, allowing you to create any type of site you want.

CULT[i]VATE includes four new widget-ready area’s and one custom accordion widget for one of the area’s. It also includes two front page templates.

This theme is for sale for $12.00 which you can purchase here. Looking for a demo?

[wp_eStore:product_id:1:end]

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Mason Rudolph Net Worth: How Rich is the NFL Player Actually?

Mason Rudolph Net Worth: How Rich is the NFL Player Actually?

Mason Rudolph is an American football quarterback who currently plays for the Pittsburgh Steelers of the National Football League. As of 2019, Mason Rudolph net worth is estimated to be $4 million. Rudolph was born on July 17, 1955, in Rock Hill, South Carolina and was named after his father and grandfather. He went to […]

Lil TJay Net Worth: 5 Interesting Facts About The Rapper

Lil TJay Net Worth: 5 Interesting Facts About The Rapper

Tione Merritt, who is better known by his stage name as Lil Tjay is a young and rising American rapper and songwriter originally from the Bronx in New York. Here are some interesting facts about Lil Tjay: 1. Lil Tjay net worth is estimated to be $600,000 As of 2019, Lil Tjay net worth is […]

Jim Jordan Net Worth: How Rich is the Republican Congressman?

Jim Jordan Net Worth: How Rich is the Republican Congressman?

James Daniel Jordan, also known as Jim Jordan, is an American politician and former wrestler. He is part of the U.S. Representative for Ohio’s 4th congressional district since 2007. Moreover, he is also a ranking member of the House Oversight Committee since 2019. As of 2019, Jim Jordan net worth is estimated to be $400,000. […]

Catherine Tyldesley Net Worth: How Rich is the Actress Actually?

Catherine Tyldesley Net Worth: How Rich is the Actress Actually?

Catherine Tyldesley is an English actress who is best known for her role as Eva Price in the soap opera Coronation Street on ITV. She recently appeared in the popular show called Strictly Come Dancing along with Johannes Radebe in its seventeenth edition. As of 2019, Catherine Tyldesley net worth is estimated to be £800,000. […]

Trending