• 0

WordPress Loop Side Effect


Question

I'm using this plugin to display the "Trending" list. http://wordpress.org/extend/plugins/wp-most-popular/

The way this loop is set is causing the post loop to react to it. The first post in the trending list will end up going to the article's loop, replacing the original content.

I believe it's the "foreach". I tried doing "while", but it didn't work for me.

<?php $posts=wmp_get_popular(array('limit'=>5,'post_type'=>'post','range'=>'daily'));global $post;if(count($posts)>0):foreach($posts as $post):setup_postdata($post);?>
<li><a href="<?php the_permalink();?>"><?php the_title();?>  |  <?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>

Anyone know a way? Check out the screenshot (look at the "Trending" bar, the post, and also look at the URL and the title on the browser) and you'll know what I mean. :)

post-388684-0-84487500-1355188723.jpg

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0

Nothing? :/

You need "wp_reset_postdata();" after the most popular loop to let wordpress fetch its post(s) again. Otherwise its going to use the post which the loop you posted told it to. Which is of course what it's doing.

See here for more: http://codex.wordpre..._reset_postdata

Replace what you posted with this:


<?php $posts=wmp_get_popular(array('limit'=>5,'post_type'=>'post','range'=>'daily'));global $post;if(count($posts)>0):foreach($posts as $post):setup_postdata($post);?>
<li><a href="<?php the_permalink();?>"><?php the_title();?>  |  <?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>
<?php wp_reset_postdata(); ?>
[/CODE]

Link to comment
Share on other sites

  • 0

Even the reset didn't work. :s

really :s strange.

watch this space, gonna have a look at a few things and try replicate the problem.

Link to comment
Share on other sites

  • 0

Gotcha, it's holding every single loop there is back! :|

im guessing the plugin is up to date?

Can you remove the code from your theme for a sec and set it up as a sidebar widget? Jus to see if it fixes the problem, I installed it as a sidebar widget justnow and its working fine.

Just want to try narrow it down to the code you posted or if its something else

Link to comment
Share on other sites

  • 0

This a theme I made from scratch so I don't even have widgets installed on there.

I'm guessing wrapping it in something will cause the foreach to stay in it's place.

Link to comment
Share on other sites

  • 0

okay, I installed it as a widget and it was fine. If I paste yours or any other code that uses the plugin BEFORE my loop, it breaks all my posts too.

Put the code after the loop and its fine.

so it works in the sidebar because thats loaded after the posts.

Strange though, I thought reset_postdata would've worked ok.

Link to comment
Share on other sites

  • 0

Yea, check out http://illingspree.com on any of the posts, you'll see the original trending function on the sidebar.

I'm doing this on a localhost of course.

yeah works fine in sidebar lol

Im reading the codex on the loop, it says you cant run the loop twice and thats why were having issues. however I still cant get it fixed.

Still trying though.

Have you got anywhere

(also i meant to say, I build my own WP themes too but I still use widgets. It makes it easier for a customer to rearrange their sidebar if they wanted :))

Link to comment
Share on other sites

  • 0

lol we might have to try and convert the foreach into a while statement because the foreach doesn't have an exit. But, I already attempted to do that, and I couldn't successfully make that happen yesterday.

Link to comment
Share on other sites

  • 0

lol we might have to try and convert the foreach into a while statement because the foreach doesn't have an exit. But, I already attempted to do that, and I couldn't successfully make that happen yesterday.

yeah I did think about that, but when I tried it I fudged it and ended up with about 1000 trending posts haha!

Im not a php guru but I can usually work stuff out with google, however this is a hard one lol. I like a good problem though and im bored so its fun to try and help :)

Link to comment
Share on other sites

  • 0

yeah I did think about that, but when I tried it I fudged it and ended up with about 1000 trending posts haha!

Im not a php guru but I can usually work stuff out with google, however this is a hard one lol. I like a good problem though and im bored so its fun to try and help :)

Haha thanks mate, the way I modify my own themes for personal use is just off the charts sometimes, heck, sometimes you wouldn't even believe that it was made on WordPress because I only load files on the server side.

I'm not a PHP guru myself, but thank god for sites like Stack Exchange. lol

Link to comment
Share on other sites

  • 0

Haha thanks mate, the way I modify my own themes for personal use is just off the charts sometimes, heck, sometimes you wouldn't even believe that it was made on WordPress because I only load files on the server side.

I'm not a PHP guru myself, but thank god for sites like Stack Exchange. lol

I've done it!! :D :D

the plugin sets its own var called posts, but thats what WP uses for posts. so it was rewriting the posts and thats why they couldnt be reset/rewound.

All I had to do was change the variable name!

Heres the working code!


<?php $popular=wmp_get_popular(array('limit'=>5,'post_type'=>'post','range'=>'daily'));global $post;if(count($popular)>0):foreach($popular as $post):setup_postdata($post);?>
<li><a href="<?php the_permalink();?>"><?php the_title();?>  |  <?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>
<?php wp_reset_query();rewind_posts(); ?>
[/CODE]

Link to comment
Share on other sites

  • 0

Holy crap! :D

I really appreciate the hard work mate, this will finally boost my confidence in soon releasing the theme update! :D

not a problem mate :) gave me something to do since I was bored tonight and its funny I was actually practising some PHP earlier!

Im off to bed now as its 2.20AM haha.

Let me know if theres anything else :)

Link to comment
Share on other sites

This topic is now closed to further replies.