• Hi everybody,

    I need help to set two differents thumbnails sizes; A big one for the last article, and then, normal ones.
    Here is my custom loop, in order to identify the last post as “featured”. Right now, there is just one thumbnail size both for “featured” and “regular”.

    <?php while (have_posts()) : the_post(); ?>
    <?php if (is_paged()) : ?>
    <?php $postclass = ('regular'); ?>
    <?php else : ?>
    <?php $postclass = ($post == $posts[0]) ? 'featured' : 'regular'; ?>
    <?php endif; ?>
    <div class="<?php echo $postclass; ?>">
    <div class="thumbnail">
    	<!-- post thumbnail -->
    		<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<?php the_post_thumbnail(array(583,420)); // Declare pixel size you need inside the array ?>
    			</a>
    		<?php endif; ?>
    		<!-- /post thumbnail -->
    </div>
    <detail>
    		<!-- post title -->
    		<h2>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    		</h2>
    		<!-- /post title -->
    <!-- post details -->
    		<span class="date"><?php the_time('F j, Y'); ?> <?php the_time('g:i a'); ?></span>
    		<span class="author"><?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>
    		<span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>
    		<!-- /post details -->
    		<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    		<?php edit_post_link(); ?>
    </detail>
    	</div>
    <?php comments_template() ?>
    <?php endwhile ?>

    I tried some stuffs but it doesn’t work. I am a beginner in PHP ! :-/
    If you could help… Thanks in advance.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You should be able to use the wp_query global within the post loop, so in-turn you can check the current_post property against the post_count. Therefore if current_post == post_count, you’re on the last post.

    More on WP_Query: https://codex.www.ads-software.com/Class_Reference/WP_Query

    Quick Gist: https://gist.github.com/JayWood/a4ddde89cc0ff16bb364#file-diff-thumbnails-for-last-post-php-L14-L18

    Thread Starter ben83

    (@ben83)

    Thank you very much, it works !
    Unfortunately, I just notice <?php $postclass = (‘regular’); ?> removes the category class for each post… There is no more category because the post class is like “regular” or “featured”.
    And I used category classes to get style with CSS for each kind of post. (.category-news, videos, etc).
    PHP will kill me.
    Anyway, thanks a lot for your time & help !

    I use this to create different image sizes for use on my site:
    https://codex.www.ads-software.com/Function_Reference/add_image_size

    functions.php usage:
    add_image_size('small-thumb', 100, 100, true);

    theme usage:

    post_thumbnail('small-thumb');
    post_thumbnail('large-thumb');

    Thread Starter ben83

    (@ben83)

    Hi,

    Thank you for your answer.
    In fact, I already have a function for 3 thumbnail sizes in the function.php file.
    But I don’t know how to use them in the loop, to get the first one bigger than the other.

    Thanks a lot for your help,

    Ben

    <?php the_post_thumbnail('small-thumb'); ?>

    First make the size in functions. Then, whatever you named the size, use that in your theme. Then reupload the images, cause wp creates the additional sizes upon upload i believe so earlier images don’t count.

    Thread Starter ben83

    (@ben83)

    It’s OK for the thumbnail sizes, but how to create two differents sizes of posts ?
    For information, I have no problem with CSS, I am just lost in the PHP ??

    I guess I have to create 2 sorts of posts in my loop. I just want to create a very simple grid like that, for example : :
    The first one 100% wide, then 2 posts, then one, then 2, etc.

    Here is my basic loop :

    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
    
    	<!-- article -->
    	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
    		<!-- post thumbnail -->
    		<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    				<?php the_post_thumbnail(array(120,120)); // Declare pixel size you need inside the array ?>
    			</a>
    		<?php endif; ?>
    		<!-- /post thumbnail -->
    
    		<!-- post title -->
    		<h2>
    			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    		</h2>
    		<!-- /post title -->
    
    		<!-- post details -->
    		<span class="date"><?php the_time('F j, Y'); ?> <?php the_time('g:i a'); ?></span>
    		<span class="author"><?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>
    		<span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>
    		<!-- /post details -->
    
    		<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
    
    		<?php edit_post_link(); ?>
    
    	</article>
    	<!-- /article -->
    
    <?php endwhile; ?>
    
    <?php else: ?>
    
    	<!-- article -->
    	<article>
    		<h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2>
    	</article>
    	<!-- /article -->
    
    <?php endif; ?>

    And I don’t understand how to write a loop getting two sizes of posts, like the example below !

    Thanks a lot for your help, my PHP comprehension is getting better thanks to you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to get two thumbnails sizes in a custom loop ?’ is closed to new replies.