Multiple WordPress Loops: Back To Normality
Posted on Wednesday, July 4th, 2007
Having hacked up the front page of my WordPress theme with three loops recently, I actually broke a couple of parts of the page. The previous posts link that you can see at the bottom of the front page now, stopped working because of the three different loops I was using.
At the time I replaced them with a link to the Archives, but I later received a message from Hans to tell me that the link was both not expected to visit the archives and wasn’t as useful as a link to the previous 5 posts. At the time I said I would do something about it, displaying bad usability on this site is against what I am trying to achieve, but didn’t get the chance, until now.
Page 2 Of The Front Page
Adding the previous and next posts links back into the theme was the easy part, however clicking on them just kept returning the front page. I needed to use the conditional tags from WordPress to work out when I was on the front page. I started off with a simple
<?php if (is_home()) { ?>
// The hacked up loops
<?php } else { ?>
// The normal loop for pages other than the front page
<?php } ?>
However, this didn’t work as WordPress considers the second page of the front page to still be home and I still had the same problem.
Some Creative Searching
I headed to Google for some inspiration and after some creative searches (that I can’t replicate now) I found a forum topic on restricting Google to certain pages of your site that gave me the answer. Why a post in an SEO forum topic was returned, I have no idea, but it helped out. I had to add the following code:
<?php if (is_home() && $paged < 2) { ?>
// The hacked up loops
<?php } else { ?>
// The normal loop for pages other than the front page
<?php } ?>
Now, if you use the previous link at the bottom of the home page you are taken to a normal looking list of 5 not-quite-so-recent posts in normal format, while the front page still highlights the latest article.
Credit
Having checked through my history, it was the Webmaster World forums, where I found my answer in the post from Ma2T. I also want to thank JTK for reminding me of this fix.
If you have messed around with your WordPress loops, make sure these links still work. If they don’t, try the above. Let me know if you have any other problems with multiple loops too, I would love to help.
If you enjoyed this post, why not subscribe to Unintentionally Blank
1September 6th, 2007 at 6:43 pm
Darren Says:I love customising the wordpress loop. I think its far more interesting and gives much more control in comparison to the default loop.
2September 6th, 2007 at 11:23 pm
Phil Says:Hi Darren,
It is great fun to play with the loop and you don’t need to know too much about PHP either. Though you do have to make sure you don’t leave other pages unusable as I did for a while, until doing the above.
I wonder what the most creative hacking of the Loop has been…