1

On the home page of my site I have a series of tiles that link to various parts of the site. One tile links to the blog and I have written some code to pull the latest post with an image attached to it and output it along with the post title. Here is the code from the theme file:

$z=0; //sets post counter
query_posts(array(  'meta_key' => '_thumbnail_id'  )); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts() && $z<1) : the_post(); 


//Call a post image attachment
    $args = array( 'post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $post->ID ); 
    $attachments = get_posts($args);

    if ($attachments) {

        foreach ( $attachments as $attachment ) {
        $image = wp_get_attachment_image_src($attachment->ID,'homethumbs'); 
?>
<a href="<?php echo get_posts_page_url(); ?>" title="Visit the Serge Blog" id="homelink3" class="top_homelink"><img src="<?php echo $image[0]; ?>" width="308" height="308" id="home-blogthumb" class="side-homethumb wp-post-image" /> <p class="home_text">Blog<br/><span class="home_thumb_caption">Latest Post: <?php the_title(); ?></span></p>   </a>
<?php $z++;
}
//$usedthumbnails[] = get_the_post_thumbnail(); 
}
?>
<?php   
endwhile; 
endif; 
wp_reset_query(); ?>

I realise that this code doesn't in fact get the featured image but instead gets 1 image that is attached to a post. This is because the blog posters sometimes forgot to "set as featured image" but any pic uploaded to the post was attached automatically. With 3.5 it now seems that featured images are not necessarily attached to the posts so will be missed by the above code. What's the best approach to solve this? Perhaps by only calling the latest featured image... I can just make sure post writers add a featured image to their posts.

The site is http://sergedenimes.com

EDIT: Here is my renewed code:

<?php
global $post;
$args = array( 'numberposts' => 1, 'meta_key' => '_thumbnail_id' );
$myposts = get_posts( $args );

foreach( $myposts as $post ) :  setup_postdata($post); 
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'homethumbs' );
?>

<a href="<?php echo get_posts_page_url(); ?>" title="Visit the Serge Blog" id="homelink3" class="top_homelink"><img src="<?php echo $thumb[0]; ?>" width="308" height="308" id="home-blogthumb" class="side-homethumb wp-post-image" /> <p class="home_text">Blog<br/><span class="home_thumb_caption">Latest Post: <?php the_title(); ?></span></p> </a>

<?php endforeach; ?>

What I want to make sure is that say I posted a new post but didn't give it a featured image, would the above query just ignore it and output the last post that does have a featured image? I'm hoping that the 'meta_key' => '_thumbnail_id' arguement should do this. If I am wrong I would expect that no url of the attachment would be got and the tile on the home page would just be blank (no image).

1 Answer 1

1

You are supplying a meta_key but I think you need to give a meta_value. See this article for more information. I realize some things may have changed in 3.5, but this doesn't seem right to me. http://codex.wordpress.org/Class_Reference/WP_Query

If you want to get featured image, why not use something like this then?

if ( has_post_thumbnail() ) {
    the_post_thumbnail( 'medium', array( 'class' => "medium" ) );
}

EDIT: adding more specific use case:

if ( has_post_thumbnail() ) {
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'homethumbs' );
?>
<a href="<?php echo get_posts_page_url(); ?>" title="Visit the Serge Blog" id="homelink3" class="top_homelink">
    <img src="<?php echo $thumb[0]; ?>" width="308" height="308" id="home-blogthumb" class="side-homethumb wp-post-image" /> 
    <p class="home_text">Blog<br/><span class="home_thumb_caption">Latest Post: <?php the_title(); ?></span></p> 
</a>
<?php } ?>
3
  • Thanks, yes it turns out it's much easier just to query 1 post with a featured image and echo its url where needed. I'm placing my renewed code above. can you confirm it will only return posts with a thumbnail, else I'll end up with a blank image if I post a post without a thumbnail.
    – harryg
    Dec 18, 2012 at 19:08
  • You need a conditional to check to see if there is indeed a featured image. That's what my code above does. I have added a block that is more specific to your use case.
    – GhostToast
    Dec 19, 2012 at 16:58
  • Hi - if this was resolved then please consider updating and accepting the answer so that we can keep the site tidy. Thanks. Jul 23, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.