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.
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.
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
1. Making the Most of Image sharing Services in Wordpress
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.
Partners: Damenmode