The Loop is used by WordPress to display each of your posts. It’s the most important set of PHP codes. Basically, it’s what displays the content you see on your homepage, your single posts, pages, archives, search results, etc. Any HTML or PHP code placed in the Loop will be repeated on each post. The Loop should be placed inside index.php and in any other Templates used to display post information.

Parts of the loop

The loop has three parts:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Part 1) Here stands the content that you want to be displayed in the Loop

<?php endwhile;?>

Part 2) Here stands what is displayed when the Loop is over

<?php else : ?>

Part 3) If there’s nothing to display

<?php endif; ?>

Template Tags in the Loop

Basic tags:

if (have_posts()) – checks to see if you have any post.

while (have_posts()) – if you do have it, while you have any post, execute the_post().

the_post() – call for the posts to be displayed.

endwhile; – this is used to close while()

else – is what to do when you don’t have any post at all

endif; – close if()

Than there are more template tags within the Loop that will output things such as the post title, the permalink, the content, etc:

<?php the_permalink() ?>

– This tag echos the permalink of the post

<?php the_title(); ?>

– This one echos the post title

<?php the_time(’F jS, Y’) ?>

– This will echo the date, for example July 4th, 1776.

<?php the_author() ?>

– This will display the author’s name

<?php the_tags(’Tags: ‘, ‘, ‘, ‘<br />’); ?>

– This will display the tags assigned to the post, separated by commas, and followed by a line break

<?php the_category(’, ‘) ?>

– This will display the categories

<?php edit_post_link(’Edit’, ”, ‘ | ‘); ?>

– This will display the  edit post link which will be visible only to those with permission.

<?php comments_popup_link(’No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?>

– Will display the link to the comments. This will not be displayed on single posts or pages.

The rest of the tags are available at WordPress Codex.

Posts Nav Link

After the loop you should make the pagination that you see on the homepage, archives, and search results (but not on the single posts or pages):

<div>

<?php posts_nav_link(); ?>

</div>

where

posts_nav_link() – call for the Next and Previous links

If there are no posts to display then the following will be displayed after the <?php else : ?>:

<h2>Not Found</h2>
<p>Sorry, but you are looking for something that isn't here.</p>

Insert ads after the first post

OK, now let’s hack it just a little bit. The ads usually are displayed on the blog sidebar, footer or header, but if you think that’s not the best solution then you can insert them after the first post. You can do this by replacing your current loop with the following one:

<?php if (have_posts()) : ?>

<?php $count = 0; ?>

<?php while (have_posts()) : the_post(); ?>

<?php $count++; ?>

<?php if ($count == 2) : ?>

//Paste your ad code here

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php the_excerpt(); ?>

<?php else : ?>

<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php the_excerpt(); ?>

<?php endif; ?>

<?php endwhile; ?>

<?php endif; ?>

That’s more or less what the loop in WordPress is about, its parts, template tags, necessary “navigation” after it and maybe the “ads” too.

1 Comment

Related posts

Featured Designs

Parent Society

Parent Society

Marijuana Maps

Marijuana Maps

Cake Journal

Cake Journal

CopyKat Recipes

CopyKat Recipes

Jessica Denay

Jessica Denay

Keith Ferrazzi

Keith Ferrazzi

Rennaissance Yoga

Rennaissance Yoga

Brink Zone

Brink Zone

Illustrious Author

Illustrious Author

Boca Care

Boca Care

KingsCast

Kingscast

Notecook

Note Cook

My Business Musings

My Business Musings

One Response so far | Have Your Say!

  1. Thank u very mush for the great explaination

Leave a Reply