Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display image from post, homepage WordPress

  • SOLVED

[[LINK href="http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it"]][[/LINK]]

Code being used in functions.php
// grab first image from post
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}


Problem: Works great on subpages. Doesn't work on Home page.
Only the first post not working with grabbing the first image other images are showing up after each posts.

Then on subpage all is working as intended with same posts.

SUBPAGE THEME CODE:
For subpages we want it to set the "tag" by grabbing the "newstag" custom field attached to that particular page.
<?php
$key="newstag";
$tag=get_post_meta($post->ID, $key, true); ?>
<?php if ($key != ""){?>
<?php query_posts('category_name=news&tag='.$tag.'&posts_per_page=1'); ?>
<?php } else { ?>
<?php query_posts('category_name=news&posts_per_page=1'); ?>
<?php } ?>
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<div class="subbar"><span>Latest in <?php single_cat_title(); ?></span></div>
<!-- left col -->
<div class="leftcol">
<div class="newsbox">
<div class="dateline"><span class="yellow align-left"><?php the_date(); ?></span></div>
<h1><?php echo the_title();?></h1>
<a href="<?php echo the_permalink();?>"><img src="<?php echo catch_that_image(); ?>" class="align-left frame" width="134" height="89" /></a>
<p> <?php echo strip_tags(content(121), '<p><a><h2><blockquote><code><ul><li><i><em><strong>'); ?></p><div class="iclear"></div><a href="<?php echo the_permalink();?>" class="button"></a>
</div>
<?php endwhile; else: ?>
<?php _e('Sorry, no posts matched your criteria.'); ?>
<?php endif; ?>
</div>



HOMEPAGE CODE:
We just wants all news non specific tags.
<!-- 2 Column News area -->
<div class="box-full"><?php query_posts('category_name=news&posts_per_page=1'); ?>
<?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>
<div class="subbar"><span>Latest in <?php single_cat_title(); ?></span></div>
<!-- left col -->
<div class="leftcol">
<div class="newsbox">
<div class="dateline"><span class="yellow align-left"><?php the_date(); ?></span></div>
<h1><?php echo the_title();?></h1>
<a href="<?php echo the_permalink();?>"><img src="<?php echo catch_that_image()?>" width="134" height="89" class="align-left frame" /></a>
<p><?php echo strip_tags(content(121), '<p><a><h2><blockquote><code><ul><li><i><em><strong>'); ?></p><div class="iclear"></div><a href="<?php echo the_permalink();?>" class="button"></a>
</div>
</div> <?php endwhile; else: ?>
<?php endif; ?>

Answers (1)

2011-02-07

Nilesh shiragave answers:

can you show me your website link...

may be home page query no receiving each posts first thumbnail. instead add this new function for home page


function home_catch_that_image($post) {

$first_img = '';

ob_start();

ob_end_clean();

$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);

$first_img = $matches [1] [0];



if(empty($first_img)){ //Defines a default image

$first_img = "/images/default.jpg";

}

return $first_img;
}


and call this function in image src as

<img src="<?php echo home_catch_that_image($post)?>" width="134" height="89" class="align-left frame" />


pandragon comments:

I'm working locally using Xaamp so I don't have a url to show, I'll try this though and get back to you in a sec!


pandragon comments:

Ah ok turns out it's adding ..wp-content instead of full http://localhost/wordpress/wp-content etc

I just deleted the image and reposted it and working lol :D oops!
Thanks for help though :)