How to Display Number of Posts in a Loop WordPress

How to Display Number of Posts in a Loop WordPress

Have you ever want to display a different posts on different pages. Example on to your category pages, you want to display about 10 posts then you can control from your Admin Panel but another page you want to show only five posts. Well check this tutorial.

Open a Template file where you want to display an x number of recent posts:

// if everything is in place and ready, let’s start the loop

// 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

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 ‘‘ to display post excerpts instead

// 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

Leave a Reply

Your email address will not be published. Required fields are marked *