Jump to content



Photo

WordPress Loop Side Effect

php

  • Please log in to reply
17 replies to this topic

#1 Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 11 December 2012 - 01:16

I'm using this plugin to display the "Trending" list. http://wordpress.org...p-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();?>&nbsp;&nbsp;|&nbsp;&nbsp;<?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. :)

Attached Images

  • Screen Shot 2012-12-10 at 8.17.13 PM.jpg



#2 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 11 December 2012 - 23:06

Nothing? :/

#3 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:08

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


#4 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:12

Even the reset didn't work. :s

#5 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:14

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.

#6 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:18

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

#7 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:23

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

#8 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:27

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.

#9 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:29

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.

#10 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:38

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.

#11 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:48

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

#12 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:49

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.

#13 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 01:54

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

#14 OP Mr.XXIV

Mr.XXIV

    Shine bright like Iron Man.

  • Tech Issues Solved: 1
  • Joined: 30-April 11
  • Location: Durham, North Carolina
  • OS: OS X Yosemite
  • Phone: iPhone 5s

Posted 12 December 2012 - 01:58

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

#15 Guth

Guth

    Alba Gu Bráth

  • Joined: 30-December 05
  • Location: Scotland
  • OS: Windows 8 Pro
  • Phone: iPhone 5

Posted 12 December 2012 - 02:11

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