A Guide To WordPress Loops
Eric from rougedeals.com gave me an excellent idea a few days ago. He said he hates creating loops for WordPress so it might be a good idea to make a plugin or something to generate loops automatically with an admin page that can be used to select what you want inserted into the loop. I have tried to make it work, but am finding all sorts of problems in the creation process. That doesn’t mean I’m giving up though. Until then I’ve decided to write an article explaining how loops work & giving some examples of the more complicated loops that can be created.
What Is The Loop?
The loop is a section of code used inside a WordPress theme that repeats itself until WordPress no longer has any new pages/posts to show. How many pages/posts it shows all depend on the query given to WordPress. The query differs depending on the theme page, the admin & manual query, but more on that later. Here is a loop in it’s simplest form:
1 2 3 4 5 6 7 8 9 10 11 |
if(have_posts()) : while(have_posts()) : the_post(); <h1> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_title(); ?> </a> </h1> <?php the_content(); ?> endwhile; endif; |
What Did You Mean By Theme Page?
Well the query given to WordPress for the loop differs by the theme page. If you are editing the page called single.php
then the loop will always contain the 1 post relating to the URL. If you are editing category.php
then the query will contain the posts for the category relating to the URL. Hopefully you get the idea. The names of the files are pretty much self explanatory in what they affect.
What Did You Mean By Manual Query?
Well using two methods you can override WordPress’ query to use your own. However most people want to leave the default query alone & add a second seperate one that runs along side it. This can be used to create a featured post section (without the use of a plugin), recommended posts from the same category, random posts and more. Here is an example of using a manual query:
1 2 3 4 5 6 7 |
query_posts('posts_per_page=5&category_name=featured'); if(have_posts()) : while(have_posts()) : the_post(); //code ect. endwhile; endif; |
What About Those Fancy Queries?
Well I’m going to leave those as those are tutorials in and of themselves, and will do them soon. You’ll be able to see how they work soon too when two new sites I’ve been helping develop (both running WordPress) go live. One you’d probably never believe was WordPress if you didn’t know.
Why Doesn’t My Normal Query Work After Using query_posts()?
This is generally because you have used query_posts();
before the standard WordPress query. If you do the standard WordPress query will be overwritten with the new one. If you want to add something to the standard WordPress query you can do so by merging your additions & the original query together like this:
1 2 3 4 5 6 |
query_posts( array_merge( array('orderby' => 'rand'), $wp_query->query) ) ); |
Just add any of query_posts()
normal parameters into the array. In this case I’ve told it to add random ordering of posts into WordPress’ normal query.
So It’s Quite Simple Then?
Well yes, and no. If you are code savvy then I guess it would be pretty easy to grasp the concept, but after working with someone who doesn’t have a firm grasp of PHP I can honestly see how it could be difficult for some people. My advice is to read, re-read and read again posts like this one along side the WordPress Codex. Also don’t be afraid to try things out on a page set to private with a custom page template in a working WordPress blog, practical experience (I find) is the best way to learn.
Well that’s about it. That is my try at a small guide to WordPress loops. I hope I’ve helped explain what the loop is & a few things about it. As I said in the tutorial I’ll be doing a few tutorials over the next few days showing how to do different things with the loop that I’ve learned while using WordPress. If you have any questions or anything to add feel free to leave a comment. Unhelpful or rude comments will be removed so just don’t bother leaving them.