Try Tuts+ Premium, Get Cash Back!
WordPress Cheat Sheet: The Loop Code Snippet
basix

WordPress Cheat Sheet: The Loop Code Snippet

This entry is part 1 of 4 in the series WordPress Cheat Sheets

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!


Other parts in this series:WordPress Cheat Sheets: The Loop Visual Model
Brandon Jones is MDNW on Themeforest
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • julia

    Nice. But why use gray font on a gray background? the pdf file is not legible.

  • Bertrand

    I’m new on WordPress, and I found this simple Cheat Sheets very usefull !
    Thanks for the work !

  • Fx

    thanks but your loop should be ready for internationalization… great idea anyway

    • Brandon Jones
      Author

      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!

      • Fx

        Thanks for all the non english speaking people, i’ll be waiting for the rest of the series…

  • supprof1

    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

  • http://thebettertwin.co.uk thebettertwin

    “With the popularity of our old WordPress cheat sheet”
    might want to check the link. it cant be accessed any more.

    • Brandon Jones
      Author

      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).

  • http://www.wp-tricks.co.il/ rami

    better use get_search_form() instead of include (TEMPLATEPATH . “/searchform.php”);.

  • Pingback: WordPress Cheat Sheet: The Loop Code Snippet

  • Xander

    Some people make it a sport to take the most simplest things and create massive tutorials, datasheets etc
    for it.

  • Pingback: Vu sur le web #1 – Octobre | Blog | Xbition-art

  • Pingback: WordPress Cheat Sheets (III): Theme Anatomy Model | Web Help 101

  • Pingback: WordPress: The Loop Cheat Sheet | Best Cheat Sheets

  • Pingback: Best Beginner Wordpress Theme Developer Cheat Sheets

  • http://ageprumanto.com Agep Rumanto

    I love the way you deliver it.. easily understood

  • Nathan Cyphert

    I’m trying to toggle the content of each post in my loop. But having some trouble. Anyone have any good resources on that?

  • Pingback: 15 Useful WordPress Infographics & Cheatsheets

  • Pingback: 20 Wordpress инфографиков

  • Pingback: Useful WordPress Infographics | Smashing Web Designs