【问题标题】:Related Posts code causing wrong post comments to load in Wordpress相关帖子代码导致在 Wordpress 中加载错误的帖子评论
【发布时间】:2011-10-16 15:07:15
【问题描述】:

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

我的目的是在帖子下方和 cmets 上方对齐一个 AdSense 块和 5 个随机帖子。

经过大量的尝试和错误后,我能够找到一些看起来很好对齐的东西,并且不会影响帖子布局的任何其他部分。

现在我发现 cmets 加载不正确。该帖子随机从另一个帖子加载错误的 cmets。我知道我要求在相关帖子代码中添加更多帖子,但无论如何要加载主帖子 cmets 而不是从显示的随机帖子中加载随机 cmets?

我只是无法弄清楚。我是新手,不想弄乱 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>

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: html css wordpress


    【解决方案1】:

    原因可能是$post 变量存储了您当前正在查看的帖子的信息。然后您的 foreach 循环会更改 $post 变量。事实上,您可能会发现出现的 cmets 是您的“相关帖子”部分中的最后一篇帖子。

    要解决这个问题,请在您的 foreach 声明之前输入:

    $temp = $post;
    

    然后,通过放置来重置$post 变量

    $post = $temp;
    

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

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 2013-12-02
      • 2011-05-20
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 2017-05-30
      相关资源
      最近更新 更多