0

I've a query like this:

<ul>
<?php $the_query = new WP_Query( array( 'cat' => '1', 'posts_per_page' => 5 ) ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<li>

<?php if ( has_post_thumbnail()) :  ?>
<?php the_post_thumbnail(array(230,163)); ?>
<?php endif; ?>

<a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

<?php 
endwhile;
wp_reset_postdata();
?>
</ul>

Example: https://prnt.sc/kq4nha

How can I show thumbnail the last added post?

  • As a note for the future, please add code and screenshots to the question itself. It is better to have all the information immediately available, additionally it makes it more likely to get an answer. – Nicolai Sep 3 '18 at 19:38
0

Because you're not specifying order and orderby parameters within your WP_Query query, the latest added post will be shown in the first place.

Having that in mind, you can do the following:

<ul>
    <?php $the_query = new WP_Query( array( 'cat' => '1', 'posts_per_page' => 5 ) ); ?>
    <?php $iterator = 0; ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    <li>
        <?php if ( has_post_thumbnail() && $iterator == 0 ) :  ?>
            <?php the_post_thumbnail(array(230,163)); ?>
            <?php $done_last_post_thumbnail = true; ?>
        <?php endif; ?>

        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        <?php $iterator++; ?>
    </li>
    <?php
        endwhile;
        wp_reset_postdata();
    ?>
</ul>

Of course, this will work only if the latest post has thumbnail.

0

You can use the current_post property of WP_Query, it starts from 0. Exemplary usage:

if ( $custom_query->current_post == 0 && has_post_thumbnail() ) {
  //code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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