【问题标题】:Wordpress - Recent Posts code in Header Menu breaking Post DataWordpress - 标题菜单中的最近帖子代码破坏了帖子数据
【发布时间】:2016-06-09 09:23:33
【问题描述】:

我有自定义代码,可在我网站的标题菜单中显示最新的 2 篇博文。

<?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 中查看实际博客文章时。例如,当查看标题应该显示the_title();...而不是显示实际帖子的标题时,它会显示标题菜单中最后一个“最近的帖子”的标题。

因此,不知何故,我需要使标头中的此最近帖子代码对其自身唯一,以使其不会影响实际博客帖子页面上的数据。有什么想法吗?

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    在你的while循环之后使用wp_reset_postdata

    【讨论】:

    • 你是救生员。工作!将在几分钟内接受答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多