WordPress Cheat Sheet: The Loop Code Snippet
- WordPress Cheat Sheet: The Loop Code Snippet
- WordPress Cheat Sheets: The Loop Visual Model
- WordPress Cheat Sheets: Theme Anatomy Model
- WordPress Cheat Sheets: Template Heirarchy Map
With the popularity of our old WordPress cheat sheet, we’ve decided to fire up a new batch of these quick pocket guides that you guys n’ gals can download, save to your phones for a fast reference, or even print out and keep next to your desk while you’re working on customizing WordPress to do your bidding. Today’s cheat sheet: The Loop Code Snippet!
The Loop is easily one of the most powerful and crucial pieces of WordPress to understand, and while there’s a lot already written about it, it’s also just nice to have a quick reference for the loop in the event that you need a fast refresher.
The Short Loop Snippet
This is the quick and dirty version of the loop. No frills, no extra styling, content, categories, tags, or anything like that.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content('Read the rest of this entry »'); ?>
<?php endwhile; ?>
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
The Long Loop Snippet
This example is a little bit more elaborate. It includes a lot of the core WP tags that you’re likely to use in a basic, vanilla, WordPress loop. This means that this one may be a little bit more useful as a starting point… but by no means should this be the limit to what you can do with the loop:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-">
<h2><a href="" rel="bookmark" title="Permanent Link to
<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata">Posted in <strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments
»'); ?>
</p>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php include (TEMPLATEPATH . "/searchform.php"); ?>
<?php endif; ?>
Don’t Miss…
Don’t miss out on Our Beginners Guide to The_Loop(), or our Advanced Guide to WordPress Queries, which plays right into the content of this cheat sheet.
Oh, and we’ll be doing our best to keep these sheets updated as WordPress grows into new versions, but if you spot an error (or just want to send some love), let us know in the comments!









Nice. But why use gray font on a gray background? the pdf file is not legible.
I’m new on WordPress, and I found this simple Cheat Sheets very usefull !
Thanks for the work !
thanks but your loop should be ready for internationalization… great idea anyway
Thanks FX – Keep in mind this is a barebones cheat sheet on the loop – I’ll actually be including a cheat sheet on stuff like localization and advanced queries later in this series. Cheers!
Thanks for all the non english speaking people, i’ll be waiting for the rest of the series…
not any video tutorial
what’s wrong with this website
wordpress videotutorial from html/css template to wordpress theme
i know it sounds like an impossible mission. i remember jeffry form net.tutsplus.com way was going to make a wordpress theme from psd to html to wordpress. and when he arrived to the last step (i mean html to wordpress) he canceld this last step. what a disappointment!!!!
when i saw this website coming i said finally. we are rescued thinking that this website will make it happen (a wordpress video tutorial theme). days have passed, months have passed and sonner year will pass and we see nothing coming. this is the reel deal. so are going to make it
“With the popularity of our old WordPress cheat sheet”
might want to check the link. it cant be accessed any more.
Yup – we’re still hunting for an active link on that since it looks like it was pulled down
Although we’ll be replicating all of the content on it over the next couple cheat sheets, with fully updated material (the old one was a bit out of date).
better use get_search_form() instead of include (TEMPLATEPATH . “/searchform.php”);.
Some people make it a sport to take the most simplest things and create massive tutorials, datasheets etc
for it.