1. Home
  2. Website Design
  3. Portfolio
  4. eCommerce
  5. Web Optimisation
  6. Website Video
  7. Graphic Design
  8. Blog
  9. Team
  10. Contact

Print This Page
Print this page.

Multiple Loops in WordPress Website Design

Posted by on Wednesday, June 2nd, 2010 at 11:01 am.

WordPress is a great platform, and these days it is becoming easier and easier to use it as a CMS (Content Management System); WordPress 3 is a massive step forward into this area, too. A great benefit of WordPress is the ability to have multiple loops, or to take full control over what is displayed in your main loop.

This short tutorial will show you how to get started with a WordPress function that I do not think I could live without: query_posts().

Wordpress - Implementing Multiple Custom Loops

What is query_posts()?

query_posts is a WordPress function that allows you to manipulate the loop, and even have multiple loops on one page. This is really useful if you want to filter the main loop to only display specific categories, or if you wanted a custom loop in the sidebar as well as the main loop in the main page.

Fortunately, query_posts is very easy to get the hang of. It works in almost the same way as the normal loop, found on almost all wordpress theme pages. The following is a very simple set up to start using query posts:

<?php

//The Query
query_posts('posts_per_page=5');

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

..Your content goes here..

<?php endwhile; else: ?>

..If the query parameters are not met, the alternate content goes here..

<?php endif;

//Reset Query
wp_reset_query();

?>

An important note to remember is that at the end of the function we have used wp_reset_query();, this will help to avoid conflicts with any other loops that you may have on the page.

So, where I have written “..Your content goes here..” is where you can put the standard code found between the normal WordPress loop. For example:

<h1><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h1>
<div>
 <span>Written by <?php printf( '<a href="' . get_author_posts_url( $authordata->ID, $authordata->user_nicename ) . '" title="' . sprintf( 'View all posts by %s', $authordata->display_name ) . '">' . get_the_author() . '</a>' ) ?></span> <span>on <abbr title="<?php the_time( get_option('date_format') .' - '. get_option('time_format') ); ?>"><?php the_time( get_option('date_format') ); ?></abbr></span>
 <span><a href="<?php comments_link(); ?>"><?php comments_number( 'Leave a Comment', '1 Comment', '% Comments' ); ?></a></span>
 <?php edit_post_link( 'edit', '<span>[', ']</span>' ); ?>
</div>
<div>
 <?php the_content( 'Read more &raquo;' ); ?>
</div>
<div> <span>Posted in <?php echo framework_get_terms( 'cats' ); ?></span>
 <?php if ( framework_get_terms( 'tags' ) ) { ?>
 <span>|</span> <span>Tagged <?php echo framework_get_terms( 'tags' ); ?></span>
 <?php } ?>
</div>

But obviously, this can be anything you want, using the normal WordPress functions to draw in post data.

Then you can put some text, or anything that you want to display, if the query_posts() parameters are not met; this will go where I have written “..If the query parameters are not met, the alternate content goes here..”.

Parameters for query_posts

There are many things you can set in the query_posts() function, here I will show you how to manipulate the categories, but you can find out all of the parameters by going to WordPress’ codex site, found here.

To show posts from only one category, and limit it to 3 posts, you would use the following:

query_posts('posts_per_page=3&cat=4');

As you can see, each attribute is separated by an ampersand (&), alternatively you can set each argument into an array; this technique can be found on the codex page.

To exclude a category from your custom loop, you can use the following code:

query_posts('posts_per_page=3&cat=-4');

That code will show 3 posts from every category on your blog, but exclude the category with an id of 4. You can also take this further by defining more than one category, separated by commas:

query_posts('posts_per_page=3&cat=-4,5,6,-1,-2,3');

This code would show 3 posts from categories 5, 6 and 3, and exclude categories 4, 1 and 2.

Conclusion

As you may see, this method has many benefits over the standard loop, and gives a bit of freedom to really control your WordPress Loops; this can be highly beneficial for your website design.

I hope this may help some of you out, and don’t forget you can read more about query_posts() on WordPress’ codex site, found here.

Related Posts

2 Responses to This Blog Post

  1. Joseph mccullough
    on June 3rd, 2010 Says:

    That does seem to have a lot of flexibility. You might want to prevent posts from a training video category from clogging up the home page for example. Thanks!

  2. James Kemp
    on June 4th, 2010 Says:

    It’s a great technique, especially when used with custom page templates. It allows you to pretty much do anything you need on a page. I find myself using it more and more these days!

Leave your Comment

Bookmark and Share Creare

Twitter
twitter.com/crearegroup

Recent Blog Comments

  1. Dave: Nice article. Wi...
  2. Stephen Kempin: The lack of supp...
  3. James Bavington: Hey Toby, indeed...
  4. James Bavington: Hi Yvette, I hav...
  5. yvette: Thanks for the b...

Latest Web Design Blog

Web Designers: Unobtrusive Photo Sharpening in Photoshop
Posted on Tue, 07 Sep 2010

This week, James offers an excellent method for sharpening your imagery without damaging the original image - sharpening using the standard filter can cause some unwanted artifacts.