samedi 19 septembre 2020

WP_Query - Show one random post and then another three random posts without repeating the first one

I'm doing this, using two WP_Queries. In the first one I get one post, take its ID and use it as exclude parameter fot he next WP_Query lopp. The problem is that I'm getting the excluded post again, repeating in the second loop. I really just need to show the featured image and the excerpt for the first item and then just the tile for the next three (if you have any suggestion to do everything inside the same WP_Query please don't hesitate to tell me). Thanks a lot.

<div class="posts-by-cat">

<?php 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 1,
    'orderby' => 'rand',
);
$catquery = new WP_Query( $args );

while($catquery->have_posts()) : $catquery->the_post(); 

?>

<div class="featured-image">
<?php if ( has_post_thumbnail() ) {
    the_post_thumbnail();
   
} 
?>
</div>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p><?php the_excerpt(); ?></p>
<?php endwhile; ?> 

<?php 

$exclude = get_the_ID(); 

$args = array(
    'cat'                 => 34,
    'posts_per_page' => 3,
    'orderby' => 'rand',
'exclude' => $exclude
);
$catquery = new WP_Query( $args );

// $catquery = new WP_Query( 'cat=34&posts_per_page=3&offset=1 ); 
?>

<ul class="postsbytag-listitem">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<!-- p><?php the_excerpt(); ?></p -->
</li>
<?php endwhile; ?> 
<?php wp_reset_postdata(); ?>
<ul>
</div>



Aucun commentaire:

Enregistrer un commentaire