WordPress - the Loop
Sitting bang in the middle of index.php, this is the heart of Wordpress’ functions - to print out the posts of a page.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Post Data
<?php endwhile; else: ?>
“No posts found”
<?php endif; ?>
This will display all the posts for a given page - for example, if the index page, the 10 (say) most recent posts, or if an archive page, the posts for that particular month (say). A call has gone to the database to return a given number of selected posts, in a given order.
And the second part - if no posts are available to be selected for that page, it supplies some message to indicate this…
Post content
Post content is output using the template tags. The full and rather daunting list is here.
It is possible to use other ways, but for now, why not use all the functions the wordpress developers have supplied.
Because what they output varies according to the current post [the the_post() bit ] template tags are only ever going to work in the Loop.
For example, <?php the_title(); ?> outputs the post title, as simple as that.
Similarly, the_content(), the_permalink(), etc., fairly self-explanatory.
So a very simple template for a post would be
<div class=”post”>
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2><?php the_content(); ?>
<p>Posted by <?php the_author(); ?> - <?php the_date(); ?></p>
</div>
Note div class=”post ” and not div id=”post” which won’t be valid XHTML, because most likely there will be several of them on a page.
There are more complications with template tags - to be discussed…
