开发者

Related Posts code causing wrong post comments to load in Wordpress

开发者 https://www.devze.com 2023-04-13 03:42 出处:网络
I\'ve been trying to show a related posts segment using wordpress. My intention is to align an adsense block and 5 random开发者_如何学JAVA posts adjacently under the post, and above the comments.

I've been trying to show a related posts segment using wordpress.

My intention is to align an adsense block and 5 random开发者_如何学JAVA posts adjacently under the post, and above the comments.

After a lot of trial and error, I was able to work something out that seemed to align well, and not affect any other part of the post layout.

Now I see that the comments aren't loading right. The post loads the wrong comments from another post randomly. I understand that I'm calling for additional posts in the related posts code, but is there anyway to load the main post comments instead of random comments from the random posts shown?

I'm just not able to figure it out. I'm new to this, and didn't want to mess with css stylesheets, so I've made all the changes in the single.php itself.

<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>

Any help will be deeply appreciated. Thank you!


The reason maybe is that the $post variable stores the information of the post you are currently viewing. Your foreach loop then changes the $post variable. In fact, you may find that the comments appearing are for the last post in your 'related posts' section.

To get around this, before your foreach statement, put:

$temp = $post;

and then afterwards, reset the $post variable, by putting

$post = $temp;

If this doesn't work (put global $post; before the $temp=$post; line).

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号