Jump to content
  • 0
Sign in to follow this  
Followers 0

WordPress Loop Side Effect


Question

Posted

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.


[code]<?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();?>&nbsp;&nbsp;|&nbsp;&nbsp;<?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>[/code]

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. :)

Share this post


Link to post
Share on other sites

17 answers to this question

  • 0

Posted

Nothing? :/

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355267216' post='595384918']
Nothing? :/
[/quote]

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: [url="http://codex.wordpress.org/Function_Reference/wp_reset_postdata"]http://codex.wordpre..._reset_postdata[/url]
Replace what you posted with this:
[CODE]
<?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();?>&nbsp;&nbsp;|&nbsp;&nbsp;<?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>
<?php wp_reset_postdata(); ?>
[/CODE]

Share this post


Link to post
Share on other sites
  • 0

Posted

Even the reset didn't work. :s

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355274741' post='595385238']
Even the reset didn't work. :s
[/quote]
really :s strange.

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

Share this post


Link to post
Share on other sites
  • 0

Posted

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355275139' post='595385256']
Gotcha, it's holding every single loop there is back! :|
[/quote]
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

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355276284' post='595385300']
Yea, check out [url="http://illingspree.com"]http://illingspree.com[/url] on any of the posts, you'll see the original trending function on the sidebar.

I'm doing this on a localhost of course.
[/quote]
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 :))

Share this post


Link to post
Share on other sites
  • 0

Posted

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.

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355276992' post='595385312']
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.
[/quote]
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 :)

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Guth' timestamp='1355277257' post='595385320']
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 :)
[/quote]

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
1 person likes this

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355277483' post='595385322']
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
[/quote]
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!
[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();?>&nbsp;&nbsp;|&nbsp;&nbsp;<?php the_time('F jS, Y'); ?></a></li>
<?php endforeach;endif;?>
<?php wp_reset_query();rewind_posts(); ?>
[/CODE]

Share this post


Link to post
Share on other sites
  • 0

Posted

Holy crap! :D

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

Share this post


Link to post
Share on other sites
  • 0

Posted

[quote name='Mr.XXIV' timestamp='1355278678' post='595385346']
Holy crap! :D

I really appreciate the hard work mate, this will finally boost my confidence in soon releasing the theme update! :D
[/quote]
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 :)

Share this post


Link to post
Share on other sites
  • 0

Posted

same :)

you got it! :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.