Banner
WordPress Tutorials

What is a Loop in WordPress?

What is a Loop in WordPress?

The great and powerful WordPress Loop is what makes WordPress display its posts. Depending on what theme template file is being called on when navigating your website, WordPress queries the database and retrieves the posts that need to be returned to the end user and then loops through them.

Most correctly built WordPress themes usually have the following files that contain the WordPress loop:

  • index.php
  • category.php
  • archieve.php
  • single.php
  • tag.php
  • page.php

If you open up any of these files, will contain code that may look something like this:

<?php
	if ( have_posts() ) {
		while ( have_posts() ) {
			the_post();
			// show each post title, excerpt/content , featured image and more
			the_title( '<h2>', '</h2>' );
			the_content();
		}
	} else {
		// show a message like sorry no posts!
	}
?>

The have_posts() function checks to see if there are any posts that need to be looped, and if so, the while loop is initiated. The the_post() function called first in each iteration of the loop sets up the post with all of its global variables so post-specific data can be displayed to the end user.

Article written by dudecmsadmin

dudecmsadmin

Hi there! I`m admin of ThemeGrizzly.com community where I share everything from web. I'm interested more in web design.

Leave a Reply

Your email address will not be published. Required fields are marked *

Banner