以下操作不起作用。。。
<?php $categories = get_categories( array(
\'orderby\' => \'name\',
\'parent\' => 0
) );
foreach($categories as $category):
$args = array(
\'cat\' => $category->name,
\'posts_per_page\' => 3,);
$category_posts = new WP_Query( $args );
if( $category_posts->have_posts() ): ?>
<h2><?php echo $category->name; ?></h2>
<div class="row">
<?php while( $category_posts->have_posts() ):
$category_posts->the_post(); ?>
<div class="blog-post-tile" style="background-image: url(<?php
echo get_the_post_thumbnail_url(get_the_ID(), \'full\');?>)">
<h3 class="blog-post-tile__title">
<?php the_title(); ?>
</h3>
</div>
<?php endwhile; ?>
</div>
<?php endif;
wp_reset_postdata();
wp_reset_query();
endforeach;
我的目标是在一页上显示所有现有类别的3篇最新帖子。到目前为止,该代码为每个类别显示相同的帖子。它读取良好的类别名称,只是看起来查询没有重置,它再次显示帖子。。。老实说,它在一开始就起作用了,但看起来我在代码中做了一些更改并破坏了它。我再也找不到解决办法了。