相关帖子代码导致在Wordpress中加载错误的帖子评论

时间:2011-10-16 15:07:16

标签: html css wordpress

我一直在尝试使用wordpress显示相关的帖子细分。

我的目的是在帖子附近和评论之上对齐一个adsense块和5个随机帖子。

经过大量的反复试验后,我能够解决一些看起来很好的问题,而不会影响帖子布局的任何其他部分。

现在我看到评论没有正确加载。该帖子随机从其他帖子加载错误的评论。我了解到我要求在相关的帖子代码中添加其他帖子,但无论如何都要加载主帖帖子而不是随机帖子显示的随机评论?

我只是无法解决这个问题。我是新手,并不想搞乱css样式表,所以我在single.php本身做了所有的改变。

<div style="width: 575px;">
    <div style="float: left;width: 250px;height: 250px;">
        <250x250 adsense code>
    </div>
    <div style="float: right;width: 310px;height: 250px;margin: 0px;list-style: none;line-height: 1.5em;font-size: 1em;font-weight: bold;font-family: verdana, sans-serif;margin-left: 10px;padding-top: 10px;">
        <?php $posts = get_posts('orderby=rand&numberposts=5'); foreach($posts as $post) { ?>
            <li><a href="<?php the_permalink(); ?>"><span style="color: #0000FF;"><?php the_title(); ?></span></a>
            </li>&nbsp;
        <?php } ?>
    </div>
    <div style="clear: both;"></div>
</div>

任何帮助都将深表感谢。谢谢!

1 个答案:

答案 0 :(得分:2)

原因可能是$post变量存储了您当前正在查看的帖子的信息。然后,您的foreach循环会更改$post变量。实际上,您可能会发现所显示的评论是针对“相关帖子”部分中的最后一篇文章。

要解决这个问题,请在foreach声明之前填写:

$temp = $post;

然后,通过添加

重置$post变量
$post = $temp;

如果这不起作用(将global $post;放在$temp=$post;行之前)。

希望这有帮助!