One loop to rule them all….

Sorry I had to say it. Here is a quick, simple and efficient way to change up your WordPress Loop without having to write multiple loops on a page. Let’s say for example you want to only show the content from the first post and show the excerpts from the other ones;

First let’s look at a basic loop:


// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
</div>

<?php endwhile; ?>

// Reset Post Data
wp_reset_postdata();

Now if you are not familiar with WP_Query, stop now and go read http://codex.wordpress.org/Class_Reference/WP_Query it will explain to you all the $args you can have in order to get the posts you wish to loop, but that is a separate tutorial altogether.

Let’s do something a little more unique with this loop shall we? As it is we are displaying every post’s title and content in a div with the class of “post”. Nothing that amazing, pretty standard stuff. To show only the first post’s content and the rest as excerpts we’ll need to assign a number to each post using $count:


$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
</div>

<?php endwhile; ?>

// Reset Post Data
wp_reset_postdata();

Notice the small change? First we give the $count variable the value of 0, then when we are within the loop we add one to $count via $count++ (this will make the $count value go up by one for each post). We still haven’t changed what we see though, let’s take care of that now with a simple if statement.


$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) :
?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
</div>

<?php else : ?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_excerpt(); ?>
</div>

<?php endif; ?>
<?php endwhile; ?>

// Reset Post Data
wp_reset_postdata();

There you have it, now only the first post will display it’s content while the other posts will be displaying their excerpts. Not exciting enough for you? Why not do something different for every odd/even post? Or maybe change what is displayed depending on the category?


$count = 0;
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$count++;
if ( $count == 1 ) :
?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_content(); ?>
</div>

<?php elseif ( has_category( $category ) ) : ?>

<div class="post category">
<h2><?php the_title(); ?> </h2>
<?php the_excerpt(); ?>
</div>

<?php else : ?>

<div class="post">
<h2><?php the_title(); ?> </h2>
<?php the_excerpt(); ?>
</div>

<?php endif; ?>
<?php endwhile; ?>

// Reset Post Data
wp_reset_postdata();

Notice that change there? using has_category you can check to see if the post has a category (via the Name, Slug or ID), which will return TRUE if it does or FALSE if it does not. The elseif statement is saying if it has the category (switch $category for whatever category you want to look for) then assign the class of ‘category’ (again use whatever you wish here) to the div. This way you can use css do change things up however you want, give it a background color, change the size, etc. Or add to what is displayed, show the tags, author, your imagination is your only limit.

So what’s the benefit of doing this? Why not just do multiple loops? The less queries you process the better your site will run, if/elseif statements are some of the least taxing processes you can run and with all of the possible checks you can do with WordPress’ Functions you can really go crazy with them. Of course there might be a limit you’d want to have with this but I haven’t found any documentation saying an exact number, just keep your code clean so that troubleshooting isn’t going to be a nightmare.

An example of this is on my homepage, I didn’t go to crazy with it, just showing the first post (which will be the newest) full content and the others as excerpts. I also gave the tutorial posts a different color header but that’s all for now while I brainstorm where I am going to go with the design (things are always changing so this will probably not be the same in a week or so.

I hope this gave you some ideas on what you can do within the loop, Cheers!