php - 在 Wordpress 中使用 WP_Query 在循环中显示帖子时遇到问题
问题描述
我在尝试使用 WP_Query 显示当前的 4 个帖子时遇到问题。但是,较早的帖子(第一篇文章)也显示在第一个循环中。
这是我的代码:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
如何解决这个问题?我的代码有问题吗?谢谢你。
解决方案
如果指定'posts_per_page' => 3
返回 4 个帖子,则几乎 100% 确定第一个帖子是置顶帖子。使用选项ignore_sticky_posts
忽略它们。
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
推荐阅读
- typescript - 花括号内的赋值是什么意思?
- blazor - 需要建议将视频聊天功能集成到我的 Blazor 应用程序
- python - Python中的Alteryx MD5_ASCII()函数等效解决方案
- font-size - 赛普拉斯字体大小的最后一位小数差异
- python - 为什么 LightGBM 会输出这个:完成的加载模型,总使用的 X 次迭代?
- php - 经典 MVC 应用程序中的 PHP + Jquery + cypress.io 设置。如何?
- typescript - 在 Type 脚本中创建 Dictionary 时使用常量作为键
- python - 对 pandas 数据框的操作
- sql - flutter_moor 过滤选择查询在 where 中使用多个值
- ruby-on-rails - Ruby/Rails 自相矛盾,我该怎么办?