我设置了一个自定义字段,使用页面边栏上的WP_Query拉取指定的类别ID来显示。它拉出正确类别中的帖子,但跳过最近的帖子。下面是代码片段:
<?php
$catID = get_field ( 'category_id_posts' );
$catquery = new WP_Query( 'cat='. $catID .'&posts_per_page=5' );
?>
<?php if($catquery->have_posts()) : $catquery->the_post(); ?>
<div id="recent-posts-2">
<h3 class="widget-title">Recent Posts</h3>
<ul class="nav flex-column">
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li class="nav-item">
<a class="nav-link" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php
endif;
wp_reset_postdata(); ?>即使我简化了WP_Query并删除了变量,如下所示:
$catquery = new WP_Query( 'cat=7&posts_per_page=5' );它仍然跳过该类别中的最新帖子。
任何有见地的人都会非常感谢,谢谢!
发布于 2020-01-17 13:44:42
您需要检查$catquery->have_posts(),例如-
<?php if ( $catquery->have_posts() ) : ?>
<?php while ( $catquery->have_posts() ) : $catquery->the_post(); ?>
..
..
..
..
<?php endwhile; ?>
<?php endif;
wp_reset_postdata();
?>发布于 2020-01-17 07:00:05
你不会得到第一篇文章,因为你跳过了它。
检查这一行:
<?php if($catquery->have_posts()) : $catquery->the_post(); ?>您检查您的查询是否有post,如果有,您将获取第一个。但是,您并没有打印出来。比你打来的..。
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>..。再来一次。这将给你第二篇文章,而你跳过了第一篇文章。直接解析第一个或删除第一个抓取。
https://stackoverflow.com/questions/59778767
复制相似问题