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

Tips & Tricks

Remove the title attribute using jQuery

Published

on

In WordPress, when you use wp_page_menu your anchor attribute’s usually carry a title with the same name. I’m not sure if it’s correct to do this, but it bother me when I hover over a page menu navigation link and I get a hover of the title.

So on my site I used jQuery to remove the title:

$("#nav a").removeAttr("title");

Pretty simple huh?

Continue Reading

Cult

In case you missed it, ma.tt is all new!

Published

on

You should head over the the newly redesigned site of Matt Mullenweg, the inventor of WordPress! His site is all new for the Spring season, and he has been tounting many of us with quick screen shots at WordCamp Las Vegas & WordCamp Denver. But finally, and I guess a little delayed, the new theme has launched.

I like it! What do you think?

Continue Reading

News

Working on a new theme called WordCult

Published

on

So I have been really busy, and haven’t been able to put up a new post since I got back from WordCamp Denver.

WordCult Screenshot

WordCult Screenshot

Working on some clients site’s and also a WordPress theme!

I have finished about 80% of the theme which is based off my current theme located on my personal blog site: TheFrosty. TheFrosty is using version 0.1 of the theme, which has many faults and bugs. I have fixed many of them, and probably added a few others.

In version 0.2 I’ve added a new jQuery “featured posts” loader and the option for sticky posts. I have also fixed a lot of CSS errors, it should W3C comply :).

Also in the newer version I have tried to add more to the admin panel, in ways of options.

If you would like to download this theme and test it out before I release it to the community please let me know. I would love to get some feedback or ideas on what you’ve got to say. Just use the contact form or send me a message on Twitter.

Download it:

[download#2#format=1]
[download#1#format=1]
Download Version 0.3

Once you’ve got the theme..

Let me know what you think! Leave you comments and feedback. I am also trying to get a forum up on the site as well.
Thanks!
Frosty

Update for 0.2:

I’ve updated my personal site: TheFrosty to the latest version of WordCult (0.2). I’ve already found some small bugs and CSS fixes that need to be taken care of. Also I don’t think that the Adsense display is working correctly.
If you’ve noticed any issues please contact me or leave a comment.

 

Update for 0.2.1:

The new version, 0.2.1 brings in some integration from Justin Tadlocks Widgets Reloaded plugin. It’s fully integrated into the theme. So you’ll notice some widgets disappear and be replaced by others. If you need to get them back Justin makes a plugin that will “release” the old widgets on your new theme install.

 

Update: 0.3

Get the newest version of WordCult, download Version 0.3 from this page.

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Ben Zobrist Net Worth: 4 Interesting Facts You Should Know

Ben Zobrist Net Worth: 4 Interesting Facts You Should Know

Ben Zobrist is an American professional baseball second baseman and outfielder who currently plays for the Chicago Cubs of Major League Baseball. Early Life Ben was born on May 26, 1981, in Eureka, Illinois to Cynthia and Tom Zobrist. He started playing baseball when he was eight years old. He graduated from Eureka High School […]

Angel Strawbridge Net Worth: How Rich is Angel Strawbridge Actually?

Angel Strawbridge Net Worth: How Rich is Angel Strawbridge Actually?

Angel Strawbridge is a designer, author who appears in the popular TV series called Escape to the Chateau along with her husband Dick Strawbridge where they work on renovating a massive Chateau they had bought in France after moving from England. Angel Strawbridge net worth is under review and is not available at the moment. […]

Alexandra Grant Net Worth: How Rich is Keanu Reeves’ Girlfriend?

Alexandra Grant Net Worth: How Rich is Keanu Reeves’ Girlfriend?

Alexandra Grant is an American art designer who examines language and written texts through painting, drawing, and sculpture. She is recently in the news after she was spotted holding hands with famous actress Keanu Reeves. Alexandra Grant net worth currently is under review Grant was born in 1973, in Fairview Park, Ohio. Her parents divorced […]

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg is the mayor of South Bend, Indiana. He is also an American war veteran and a former consultant. In the recent days, Buttigieg has been growing his name with him announcing candidacy for the Presidential election in 2020. Then President Obama called him as one of four Democrats who represented the future of […]

Trending