Filipe Tavares
Filipe Tavares

Reputation: 13

Get latest two post thumbnails on Wordpress

what I'd like to do is to get the latest two posts on a specific category and show their thumbnails and title on top of the blog index.

Here's how I'd like to look like: How it should look like

Here's how it is now: How it is now

Code:

    <div class="destaques">
    <?php $args = array(
    'category_name' => 'Destaques',
    'posts_per_page' => 2,
    'order_by' => 'date',
    'order' => 'desc'
    );

    $post = get_posts( $args );
        if($post) {
            $post_id = $post[0]->ID;
            if(has_post_thumbnail($post_id)){
            echo get_the_post_thumbnail( $post_id, array(379, 240), array('class' => 'post_thumbnail') );
            echo the_title ();
            }
            } ?>
</div>

How can I get two instead of just one? I can take care of styling if I can get them both to show.

Upvotes: 1

Views: 39

Answers (1)

Umar Hayat
Umar Hayat

Reputation: 121

Instead of single if block. You need to run a loop on two fetched posts.

A Simple Loop example is this

$myposts = get_posts( $args );

foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; 
wp_reset_postdata();?>

Upvotes: 1

Related Questions