I am trying to display 3 post_thumbnails from a custom post type “portfolio” on my homepage template. So one from post ‘A’ , one from post ‘B’ and one from post ‘C’ The image from post A will be set as post_thumbnail(‘large’) while B & C need to be set to post_thumbnail(‘thumbnail’).
I’ve set a custom WP_Query loop and as expected it is looping through and fetching three instances of the post thumbnail for each of the most recent three “portfolio” posts. I’m just not sure how to modify the loop to only display three images not three sets of three images.
Any help much appreciated.
Here’s my code:
<?php get_header(); ?>
<div id="content" class="clearfix row">
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<section id="work" class="row">
<div class="span3">
<h3>Recent Work</h3>
<p>Intro Lorem ipsum</p>
</div>
<?php $first_query = new WP_Query('post_type=portfolio'); while($first_query->have_posts()) : $first_query->the_post(); ?>
<div class="span6">
<a href="single-project.php">
<?php the_post_thumbnail('medium'); ?> <!--Should be from custom post 'A' -->
</a>
</div>
<div class="span3">
<a href="single-project.php" >
<?php the_post_thumbnail('thumbnail'); ?><!--Should be from custom post 'B' -->
</a>
<a href="single-project.php">
<?php the_post_thumbnail('thumbnail'); ?><!--Should be from custom post 'C' -->
</a>
</div>
Try this:
and don’t forget to set the thumbnail in
functions.php
as mentioned in David’s answer.One more thing I noticed in your code: you said that your post type is “portfolio” and in the permalink you used
single-project.php
, but shouldn’t it besingle-portfolio.php
?mattc,
You’re problem is that you have all of the elements inside the loop. You need to set a counter and then only apply the items you need when they are needed. I’ve only included the pertinent code below:
I use this implementation nicely — You can see how it’s grabbing it:
$thumbnails = get_posts(array('numberposts'=>1,'orderby'=>'ASC','meta_key' => '_thumbnail_id'));
You can then turn to ‘numberposts’=>3 and build your thumbnail structure:
Edited: Don’t forget to set your thumbnail size:
echo get_the_post_thumbnail($thumbnail->ID, 'small-thumb', array( 'alt' => esc_attr( $post->post_title ), 'title' => esc_attr( $post->post_title ) ));
Change the ‘small-thumb’ to whatever you’ve set, if you need to set some sizes you can put this in your functions.php:
Cheers,