• Resolved Aaron Kittredge

    (@kittyridge)


    Can anyone provide a function to specify the featured image size for the first (most recent) post only? The featured image size needs to be untouched for all the other posts. Only the first post in the loop on page 1 needs a special featured image size. Below was my very lame attempt at it, which not surprisingly did not work:

    if (have_posts() and has_post_thumbnail()) :
    $post = $posts[0]; $c=0;
    while (have_posts()) : the_post();
    $c++;
    if( !$paged && $c == 1) :
    the_post_thumbnail('full');
    endif;
    endwhile;
    php endif;

    Any help appreciated as this is obviously over my head.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You actually not that far off, but if you only want to query posts with thumbnails you should modify your query to do so. For example you could use query_posts() to do this. Like so:

    global $query_string;
    query_posts($query_string . '&meta_key=_thumbnail_id&meta_compare=EXISTS' );

    This is not an optimal way to do this, so you should look in to the pre_get_posts hook: https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    Remove the has_post_thumbnail() from your opening line there, as this is only availiable inside the loop if you dont set a post ID as a parameter.

    An example of how your code could look:

    <?php
    
    global $query_string;
    query_posts($query_string . '&meta_key=_thumbnail_id&meta_compare=EXISTS' ); // will only query posts with thumbnails
    
    if( have_posts() ) :
    
    	$i = 0;
    
    	while( have_posts() ) : the_post();
    
    		$i++; // count
    
    		if( !$paged && $i == 1) { // assumes you already have a $paged-variable and it is not set
    
    			the_post_thumbnail( 'full' ); // print a big image for the first post
    
    		}
    		else {
    
    			the_post_thumbnail( 'thumbnail' ); // print thumbnails for the rest
    
    		}
    
    	endwhile;
    
    endif;
    
    ?>

    Thread Starter Aaron Kittredge

    (@kittyridge)

    Thanks for reworking this snippet. I still wasn’t able to get it working, which probably had something to do with the StudioPress theme I was using. I posted over at StudioPress support forum and got a different function to put in my functions.php file that did the trick, but it only works for Genesis themes. For anyone else looking for the solution, you can find it over at https://wpsites.net/web-design/customize-the-first-featured-image-in-the-home-page-loop/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom featured image size on first post only’ is closed to new replies.