Creating multiple loops might harm your WordPress blog


Alternatives to the Loop

You may be tempted to create multiple loops or advanced loop queries in your theme’s template files when you’ve figured out how it works. However, while that may be the solution to what you want to do in some cases, it probably isn’t in all. Doing a lot of funky stuffwith the loop, often utilizing query_posts(), is sometimes completely unnecessary. Here’s why.

First of all, you really should ask yourself if it really is another loop you need. Often there are template tags that can do for you what you need accomplished, and that is almost always a better solution. Custom loop magic should be saved for actions that truly deserve and need them. The same goes for conditional tags; you can build pretty advanced pages that rely on the situation that could possibly get the result your want.

Second, are there any plugins that can do the work for you? The WordPress community is full of brilliant solutions to common problems, and while it may both seem and indeed be a lot cleaner to sort these things from within your own theme, a plugin may in fact enhance the functionality and be easier for you to manage. A lot of the plugins that do things with post listings are in fact doing stuffwith the loop that you could do yourself, from within your theme.

Normally I would recommend the latter solution, but what if you’re not the one responsible for maintaining the theme in the long run, as is common when doing theme design work and then leaving it to the client to nurture? If the client is reluctant to pay more for customizations, or if they think that their in-house HTML skills are enough, then you’re probably better offfinding a plugin solution than one that will break when they do something with their template files. Granted, plugins come with their own issues, like suddenly not being developed anymore, or relying on WordPress functionality that isn’t favored, but still, it is something to consider.

Finally, there is the possible strain on the server and database. Doing loop after loop means a lot of PHP and SQL queries and that will slow down the site. Simple is not always best, but keeping things as simple as possible while managing to get the required result is always a good idea. Both Web hosts and WordPress have come a long way since the early days, but that doesn’t mean that you should make things more clunky or complicated than they need to be. My point and advice is this: always question the need for that extra loop. It may save you some headaches in the future.

Multiple Loops

Sometimes you want several loops on a page. Perhaps you have a category for your extremely important posts, and want to run those by themselves on the front page, or maybe you just want the latest posts in the sidebar. Either way, whenever you need to fetch posts a second time, you’ll want another loop. This is often essential when you want to break from the traditional weblog mold, so you may as well master it. Start with the most basic multiple loop solution, which is to just get another loop on the page. This is done with rewind_posts(), which just resets the loop’s counter and lets you run it again:

 <?php rewind_posts(); ?>
   <?php while (have_posts()) : the_post(); ?>
   <!  And then the basic loop continues...  >

Naturally, this would just output the exact same thing as your first loop, which would neither look good nor be particularly useful, so to actually do something with rewind_posts() you need to change something. Say you want a box at the bottom of the page showing the last five posts from the News category; this can be achieved by using query_posts, which was touched upon earlier:

<?php rewind_posts(); ?>
   <?php query_posts('category_name=news&showposts=5'); ?>
   <?php while (have_posts()) : the_post(); ?>
   <!  And then the basic loop continues….  >

This would then output the five latest posts from the News category, which we would put in the box mentioned before.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.

Note: This article was sent to us by: Sean Jovers at 05152010

Related Articles

1. Making the Most of Image sharing Services in Wordpress
Making the Most of Image-sharing Services For sites running on limited hardware or shared hosting accounts, it may be crucial to save on both space and bandwidt...

2. Show off Your Twitter for Wordpress
Integrating the social web Before digging into the various techniques used to display and connect to the social Web, it is important not to forget the most ob...

3. How to do lifestreaming with WordPress
Lifestreaming with WordPress Lifestreaming is a term commonly used to describe the mashing up of everything you do online. A bit presumptuous, perhaps, to assum...

4. Social Web Submit Buttons plugin Worspress
Social Web Submit Buttons The social Web is certainly a big consideration when working with editorial content today. While search engines can trickle in visitor...

5. Wordpress Hosted Comment Solutions
Pimping the Comments With the addition of threaded comments in the WordPress core, and the excellent CSS styling options that are now available, as well as the ...

6. How to Backing Up database for Wordpress
Backing Up Anyone who has lost data in a hard drive crash or similar knows the importance of backing up, and it goes without saying that this applies to your on...

7. What you need to know about the Wordpress database
Hacking the Database Most of the time you needn’t worry about the database; WordPress will do that for you. There are database changes between versions so...

8. Switch hosts with wordpress when export and import do not work
WordPress and Switching Hosts There are several ways of moving to a new server. My preferred one is using the Export/Import functionality found under Tools in W...

9. Do not share your MySQL user with any other system
Server Stuff The MySQL user for your WordPress database, which incidentally shouldn’t be shared with any other system, doesn’t actually need all wri...

10. What is WordPress core and how to use template tags
About the WordPress Core Any good CMS will keep its core files apart so that you don’t ruin the code that makes the system work, and WordPress is no excep...