Wordpress - Header 菜单中断 Post 数据中最近的 Posts 代码

Wordpress - Recent Posts code in Header Menu breaking Post Data

我有自定义代码,可以在我网站的 header 菜单中显示最新的 2 个博客 post。

<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=2');
while ($recentPosts->have_posts()) :
$recentPosts->the_post();
?>

<?php the_permalink(); ?>
<?php the_post_thumbnail_url('thumbnail'); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>

<?php endwhile; ?>

它运行完美,除了我在 Wordpress 中查看实际博客时 post。例如,当查看博客 post 时,标题应该显示 the_title();...而不是显示实际 post 的标题,而是显示上一个标题"recent post" 在 header 菜单中。

因此,我需要以某种方式使 header 中的最近帖子代码成为其自身唯一的代码,使其不会影响实际博客 post 页面上的数据。有什么想法吗?

在 while 循环后使用 wp_reset_postdata