WordPress Default Loop, The Loop

WordPress Loop is the most basic way to get the latest posts.

We can use this loop on both index.php and single.php or page.php or archive.php

 

There are so many ways to write a loop.

Example Loop 1

<?php 
if ( have_posts() ) {
     while ( have_posts() ) {
          the_post(); 
          //
          // Post Content here
          // like the_title, the_content functions
          //
     } // end while
} // end if
?>

 

Example Loop 2

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

	<?php if ( in_category( '3' ) ) : ?>
		<div class="post-cat-three">
	<?php else : ?>
		<div class="post">
	<?php endif; ?>

	<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

	<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>

	<div class="post-content">
		<?php the_content(); ?>
	</div>

	<p class="postmetadata">
           <?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
	</div> 


<?php endwhile; else : ?>
	<p>Sorry, no posts here.</p>
<?php endif; ?>

 

Example Loop 3

A compact way to write it.

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

<?php the_title(); ?>
<?php the_time(); ?>
<?php the_content(); ?> or <?php the_except(); ?>

<?php endwhile; else : ?>
     <p>Sorry, no posts here</p>
<?php endif; ?>

 

 

 

 

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments