I have a php code in my Wordpress news site which will show 4 different posts from 2 categories News and Events at a time in a container. But my current code shows only one post of one category 4 times although there are 4 posts in news and events category. I want to show 4 different posts from news and events in the container instead of 1 post shown 4 times that is happening now. Here is my code snippet:
<div class="dc-news-trend">
<?php
$args = array( 'posts_per_page' => 4, 'category' =>'50,52', 'orderby'=>'date','orer'=>'DESC');
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<div class="row margin-top-1">
<div class="col-lg-4 col-md-4 col-sm-6 col-6">
<?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
</div>
<div class="col-lg-8 col-md-8 col-sm-6 col-6">
<a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
<p class="dc-news-date">
<?php echo get_the_date(); ?>
</p>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-12">
<div class="row margin-top-1">
<div class="col-lg-4 col-md-4 col-sm-6 col-6">
<?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
</div>
<div class="col-lg-8 col-md-8 col-sm-6 col-6">
<a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
<p class="dc-news-date">
<?php echo get_the_date(); ?>
</p>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<div class="row margin-top-1">
<div class="col-lg-4 col-md-4 col-sm-6 col-6">
<?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
</div>
<div class="col-lg-8 col-md-8 col-sm-6 col-6">
<a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
<p class="dc-news-date">
<?php echo get_the_date(); ?>
</p>
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<div class="row margin-top-1">
<div class="col-lg-4 col-md-4 col-sm-6 col-6">
<?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
</div>
<div class="col-lg-8 col-md-8 col-sm-6 col-6">
<a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
<p class="dc-news-date">
<?php echo get_the_date(); ?>
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
?>
</div>
inside your query args use cat=>'50,52' not category=>.... :
<div class="dc-news-trend">
<div class="row"> // I suppose you don't want new row for each post so we take it out from the loop
<?php
global $post;
$args = array(
'post_type' => 'post',
'cat' => '50,52',
'posts_per_page' => 4,
'orderby' => 'date',
'orer' => 'DESC'
);
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) : setup_postdata( $post ); ?>
//now we are inside foreach loop so we need to output div with the content just 1 time (for each returned post)
<div class="col-lg-6 col-md-6 col-sm-12 col-12">
<div class="row margin-top-1">
<div class="col-lg-4 col-md-4 col-sm-6 col-6">
<?php the_post_thumbnail('full', array( 'class' => 'img-fluid')); ?>
</div>
<div class="col-lg-8 col-md-8 col-sm-6 col-6">
<a href="<?php the_permalink(); ?>" class="dc-news-link"><?php the_title(); ?></a>
<p class="dc-news-date"><?php echo get_the_date(); ?></p>
</div>
</div>
</div>
<?php endforeach;
wp_reset_postdata();
?>
</div>
</div>
This will return posts that have these categories. Check the query args in the WP documentation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.