我正在尝试获取博客帖子,但我想限制在某些特定区域填充的帖子数量。它不应该有重复的帖子。
我能够得到一些结果,但是精选文章似乎不正确,并且在正确的文章上,帖子只是重复底部文章。
我使用 $query->current_post % 2 == 0
作为过滤器,但不确定这是正确的方法还是有更好的方法。
精选文章 - 1 篇
底部文章 - 3-4 个帖子
正确的文章 - 2-3 个帖子
是这样的: https://wireframe.cc/JwKlt4
这是我目前得到的。
<section class="articles-block">
<div class="featured">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => 'ASC',
'post_status' => 'publish',
'cat' => 1
);
$query = new WP_Query($args);
if ( $query->have_posts() ) {
if($query->have_posts()) : $query->the_post();
$hidepost = $post->ID;
?>
<article class="featured-article">
<!-- Thumbnail -->
<a href="#" class="article-card__thumbnail">
<img src="" alt="Thumbbnail">
</a><!-- ./Thumbnail -->
<!-- Category Label-->
<div class="article-card__category-label">
<span class="bg-blue">Uncategorized</span>
</div><!-- ./Category Label-->
<div>
<h2 class="article-card__title"><a href="#" class="article-card__title__link"><?php the_title();?> <?php echo $hidepost;?></a></h2>
</div><!-- ./Details-->
</article>
<?php endif; }?>
<div class="category-articles-block__bottom">
<?php
$query->rewind_posts();
if ( $query->have_posts() ) {
while($query->have_posts()) : $query->the_post();
$bottom = $post->ID;
if($query->current_post % 2 == 0)
continue;
?>
<article class="article-bottom">
<!-- Thumbnail -->
<a href="#" class="article-card__thumbnail">
<img src="" alt="Thumbbnail">
</a><!-- ./Thumbnail -->
<div>
<h2 class="article-card__title"><a href="" class="article-card__title__link"><?php the_title();?> <?php echo $bottom;?></a></h2>
</div>
</article>
<?php endwhile; } ?>
</div>
<!-- ./Bottom -->
<!-- Right column -->
<div class="category-articles-block__right">
<?php
$query->set('post__not_in', array($left, $dontshowpost));
$query->rewind_posts();
if ( $query->have_posts() ) {
while($query->have_posts()) : $query->the_post();
$right = $post->ID;
if($query->current_post % 4 == 0)
continue;
?>
<article class="article-right-block">
<!-- Thumbnail -->
<a href="#" class="article-card__thumbnail">
<img src="" alt="Thumbbnail">
</a><!-- ./Thumbnail -->
<div>
<h2 class="article-card__title"><a href="" class="article-card__title__link"><?php the_title();?> <?php echo $right;?></a></h2>
</div>
</article>
<?php endwhile; } ?>
</div>
<!-- Right column -->