php - 多个循环:从循环 2 中排除循环 1 中返回的帖子
问题描述
我有两个 wp_query 循环。
第一个循环查找精选帖子并将其格式设置为与其余帖子不同。
然后第二个循环显示其余的帖子。
我想从循环的其余部分中排除第一个特色帖子。
现在,第一个帖子的帖子 ID 保存为变量。
我想在第二个循环中使用该变量,并带有 wp_query 排除参数。
但据我了解,当我的第一个循环结束时,该变量就会消失。然后它在第二个循环中是一个空变量,因此不排除任何内容。
<!-- Here's the query for the featured post -->
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
'meta_key' => 'featured_post',
'meta_value' => '1'
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- If there is no featured post, pull in the most recent post and make it big -->
<?php else: ?>
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<!-- End of the nested loop (most recent posts) -->
<?php endwhile; ?>
<?php endif; ?>
<!-- End of the featred post query loop-->
<?php endif; ?>
这是主循环:
<?php get_template_part('parts/content', 'featured-post'); ?>
<!-- This ends the logic for the featured post. Now we show the rest of the posts, excluding the first one we showed -->
<?php get_template_part('parts/content', 'category-filter'); ?>
<?php
// the arguments
$args=array(
'paged' => $paged,
'posts_per_page' => 9,
'post__not_in' => array($postid)
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- Start row that holds blocks -->
<div class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="column entry" >
<?php if( get_the_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('blog'); ?>
</a>
<?php else : ?>
<?php endif; ?>
<a href="<?php the_permalink(); ?>"><h5><?php the_title(); ?></h5></a>
<?php
$excerpt = get_the_excerpt();
echo wp_trim_words( $excerpt , '10', '');
?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>Sorry, no more posts.</p>
<?php endif; ?>
我该如何设置要在第二个循环中使用的变量?
解决方案
推荐阅读
- c# - 需要帮助提高 SQL DELETE 性能
- windows - 当您作为修饰键时,我的功能/Fn 键不起作用
- java - 如何在不知道 DPI 的情况下获得以英寸为单位的图像尺寸?
- javascript - 尝试使用间隔更改 React Hooks 中的状态
- javascript - Paypal 付费墙自定义文本 css
- python - 不处理词云停用词
- sql - Redshift 跳过 split_part() 的第一个字符
- javascript - Formik 的表单无法识别 Material UI 组件的文本字段值?
- r - 以编程方式循环列表
- django - 异常值:__init__() 得到了一个意外的关键字参数“limit_choices_to”