我正在使用“高级自定义字段”来设置“精选帖子”。
我运行查询以首先检查是否有精选的帖子,如果有,请显示它。如果不是,则显示最新的时间顺序帖子。
(此外,精选帖子仅显示在分页的第一页上。)
此处的tl是:
我查询特色文章。如果没有,则在同一if循环内,我对最近的帖子运行第二个查询。
所以我在这里有一个嵌套循环。
在那之后,我只运行了一个普通的WP循环,显示了其余的帖子。
===============
我的问题是:
我确定有更好的方法可以做到这一点;我将不胜感激如何改进代码。
无论返回什么帖子,我都不希望以后再显示。我知道我在下面做的不正确(循环中有一个变量)。如何阻止该帖子显示两次?
===============
这是我的代码:
<!-- Checks if it's the first page. If it is, render the featured post -->
<?php if (!is_paged()) : ?>
<div class="" id="featured-post">
<div class="row">
<!-- 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" id="">
<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" id="">
<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; ?>
<!-- End the rows and columns for the featured post -->
</div>
</div>
<!-- We only need the "featured" header on the first page, so we end the paged if and move on to the normal query -->
<?php endif; ?>