我已經設置了一個自定義字段來提取指定的類別ID,以便在頁面側欄上使用WP_Query來顯示。它會將帖子拉入正確的類別,但會跳過最新的帖子。以下是代碼片段:<?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' );它仍然會跳過該類別中的最新帖子。任何見解將不勝感激,謝謝!
2 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
您需要檢查$catquery->have_posts()例如 -
<?php if ( $catquery->have_posts() ) : ?>
<?php while ( $catquery->have_posts() ) : $catquery->the_post(); ?>
..
..
..
..
<?php endwhile; ?>
<?php endif;
wp_reset_postdata();
?>

倚天杖
TA貢獻1828條經驗 獲得超3個贊
您沒有收到第一篇文章,因為您跳過了它。
檢查此行:
<?php if($catquery->have_posts()) : $catquery->the_post(); ?>
您檢查您的查詢是否有帖子,如果是,您將獲取第一個。但是,您沒有打印該版本。比你在打電話...
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
...再。這將為您提供第二個帖子,并且您跳過了第一個帖子。直接解析第一個或刪除第一個提取。
- 2 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消