php - Wordpress - 嵌套 WP_Query 循环重复内容
问题描述
我有一个嵌套的 wordpress 循环来生成包含两个不同类别内容的两列布局。发生的事情是内容重复,但显示了所有帖子。我试过使用wp_reset_query()
但wp_reset_postdata()
没有成功。有解决办法吗?
<?php
$main = new WP_Query(
array(
'post_type' => 'post',
'category_name' => 'Notizie varie',
'posts_per_page' => 6
)
);
//var_dump( $main );
if( $main->have_posts() ):
while( $main->have_posts() ): $main->the_post();
?>
<div class="col-8 p-4 mt-3">
<img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p>
<small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small>
<small class="text-muted"><?php the_author() ?></small>
<a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a>
</div>
<?php
$side = new WP_Query(
array(
'post_type' => 'post',
'category_name' => 'Secondo piano',
'posts_per_page' => 6
)
);
if( $side->have_posts() ):
while( $side->have_posts() ): $side->the_post();
?>
<div class="col-4 p-4 mt-3">
<img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
<h4 class="card-title"><?php the_title(); ?></h4>
<p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p>
<small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small>
<small class="text-muted"><?php the_author(); ?></small>
<a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a>
</div>
<?php
endwhile;
wp_reset_query();
endif;
endwhile;
endif;
wp_reset_postdata();
解决方案
推荐阅读
- javascript - 在 ionic reactjs 组件中返回一个对象数组
- django - 带有 save_m2m 的 int() 的 Django 无效文字
- javascript - 如何从卡片列表中删除卡片(使用 API 将 Github 用户信息提取到列表中显示的卡片上)?
- python - 在 apache 24 中部署 python 烧瓶应用程序
- python - 如何更新 Docker 镜像 (GitHub)
- javascript - 如何使用按钮单击触发搜索输入事件
- python - 复选框旁边的文本,python
- python-3.x - Tweepy 函数为给定的地理位置获取 0 条推文
- linux - 为什么 /etc/hosts 中的 0.0.0.0 可以阻止某个网站?
- amazon-dynamodb - 如何使用 GSI 在 DynamoDB 中查询超过 2 个属性?