Lesson: Playing with The WordPress Loop
-
Let’s look at the options available in the WordPress Loop. There have been a lot of changes in the new v1.5 Strayhorn with new tags and improved performance.
The Loop involves PHP code that follows the instructions given by the user (by clicking on a link, for example) to access information from the database, do specific functions developed by the designer involving the data, and then return the “presentation” of the page for the user to see.
In its most simplest form, The Loop gets information from the database regarding the posts, displays them, and if the post(s) is not found, returns with a response that says “page not found”.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_date('','<h2>','</h2>'); ?>
<div class="post">
<h3 class="storytitle" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<div class="meta"><?php _e("Filed under:"); ?> <?php the_category(',') ?> — <?php the_author() ?> @ <?php the_time() ?> <?php edit_post_link(__('Edit This')); ?></div>
<div class="storycontent">
<?php the_content(__('(more...)')); ?>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>This simple Loop publishes the story title, the categories the post is filed under, the author’s name, the time of the posting, and includes a function to allow editing, visible only to the user with the user level permitting editing privileges.
The best function of The Loop is the series of conditional statements, code that asks “if this happens then do this”. These IF/ELSE statements allow The Loop to generate different results based upon the requests being made.
In a more sophisticated example from the Codex pages describing The Loop comes an example that adds some excitement to The Loop and shows off the IF/ELSE statement. It looks for posts in Category Three and changes the look of the results in that category.
Within this examples are “comments”, explanations of what is happening that is only viewed in the code and not by the user. They are surrounded by comment codes (
<!-- comment -->
).<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box will be given the CSS class "post". -->
<?php if ( in_category('3') ) { ?>
<div class="post-cat-three">
<?php } else { ?>
<div class="post">
<?php } ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the Time. -->
<small><?php the_time('F jS, Y'); ?></small>
<!-- Display the Post's Content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata">Posted in <?php the_category(', '); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else: ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p>Sorry, no posts matched your criteria.</p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>Now, the challenge is to come up with a variety of examples of how The Loop can be used. How about an example of excerpts shown on the first page, but the whole post on the post page (index.php, single.php, or post.php – whichever is being used to host the main post)? Or an example of excerpts shown on the first page, archives, and categories? Or more conditional Loops that change the resulting information like the above category example? Show off your PHP talents, folks, and let’s see some Loop examples.
- The topic ‘Lesson: Playing with The WordPress Loop’ is closed to new replies.