Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How To Override the Default WordPress Loop

How To Override the Default WordPress Loop

A WordPress Loop, or “The Loop,” as many call it, is a function in WordPress that is used to display each of your posts. Usually, the number of posts to be displayed is set in your WordPress Admin Panel Settings area under the “Reading” tab. However, what can you do to override the default WordPress Loop?

In this article, I will show you several examples of how to use The Loop and override it to display posts in several different ways. Simply follow the code examples below to get the WordPress Loop override structure that you prefer.

You can also display custom fields from a specific post outside the WordPress Loop if you want.

Display Any Number of Posts

The loop example will display any number of posts within that specific WordPress Loop. Open any template file where you would like to place the posts and add the following loop:

// if everything is in place and ready, let’s start the loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

// to display ‘n’ number of posts, we need to execute the loop ‘n’ number of times
// so we define a numerical variable called ‘$count’ and set its value to zero
// with each iteration of the loop, the value of ‘$count’ will increase by one
// after the value of ‘$count’ reaches the specified number, the loop will stop
// *USER: change the ‘n’ to the number of posts that you would like to display

<?php static $count = 0;
if ($count == “n”) { break; }
else { ?>

// for CSS styling and layout purposes, we wrap the post content in a div
// we then display the entire post content via the ‘the_content()’ function
// *USER: change to ‘<?php the_excerpt(); ?>’ to display post excerpts instead

<div class=”post”>
<?php the_title(); ?>
<?php the_content(); ?></div>

// here, we continue with the limiting of the number of displayed posts
// each iteration of the loop increases the value of ‘$count’ by one
// the final two lines complete the loop and close the if statement

<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>

Exclude Posts From Some Category

This loop example demonstrates how to hide a specific Category or Categories from being displayed:

<?php $query = new WP_Query( ‘cat=-3,-8’ ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<div class=”post”>
<!– Display the Title as a link to the Post’s permalink. –>
<h2><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>

<!– Display the date (November 16th, 2009 format) and a link to other posts by this posts author. –>
<small><?php the_time( ‘F jS, Y’ ); ?> by <?php the_author_posts_link(); ?></small>
<div class=”entry”>
<?php the_content(); ?>
</div>

<p class=”postmetadata”><?php esc_html_e( ‘Posted in’ ); ?> <?php the_category( ‘, ‘ ); ?></p>
</div> <!– closes the first div box –>

<?php endwhile;
wp_reset_postdata();
else : ?>
<p><?php esc_html_e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
=<?php endif; ?>

Style Posts From Some Category Differently

This loop example displays each post with its Title (which is used as a link to the Post’s Permalink), Categories, and Content. It also allows posts in a category with Category ID ‘3’ to be styled differently:

<!– Start the Loop. –>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<!– Test 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 is given the CSS class “post”. –>

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

<!– Display the Title as a link to the Post’s permalink. –>

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

<!– Display the date (November 16th, 2009 format) and a link to other posts by this posts author. –>

<small><?php the_time(‘F jS, Y’); ?> by <?php the_author_posts_link(); ?></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”><?php _e( ‘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><?php esc_html_e( ‘Sorry, no posts matched your criteria.’ ); ?></p>

<!– REALLY stop The Loop. –>

<?php endif; ?>

I hope this article gave you a few easy examples of how you can override the default WordPress loop in a few different ways.

Have you found a plugin that does this task? Have you had any trouble with the code when you tried to use it?

Author: Jeremy Holcombe

Growing up in Hawaii, Jeremy started his freelance writing career doing resumes, business plans, article writing, and everything in between. He now specializes in online marketing and content writing and is part of the Content Marketing Team at GreenGeeks.

Was this article helpful?

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.