LoginSign Up

wordpress latest post limit without a Plugin codex

প্রোগ্রামিং 12 months ago 26 Jul, 2019 at 2:29 am 205
Linkedin Pint
wordpress latest post limit without a Plugin codex

Let’s say your blog is set to display ten posts per page, as specified via the WordPress Admin under Settings ▸ Reading. Once set, ten posts will appear on your home page, archive pages, search results, and so on. In other words, if it isn’t a single-view page or an actual “page” page, you’re gonna get ten posts per page. It’s a global setting.

But what if you want to display different numbers of posts for different types of page views? For example, instead of showing just ten posts on your search-results pages, you may want to show a whole bunch, like maybe fifty or something. Perhaps you would also like to limit the number of posts displayed on your category archives to only five.

Of course, many of us know the easy way to do this: install Custom Query String Reloaded1 or Custom Post Limits, tweak a few settings, and call it good. But.. this does require commitment to yet another plugin, which is a good reason for doing things the “hard” way..

Method 1

Fortunately, we can account for the latter scenario without relying on a plugin. Normally, the loop controls the number of posts displayed via the while(have_posts()) condition, as shown in this extremely boring example of a typical loop:

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

	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
	<p>?php the_time(); ?></p>
	<?php the_content(); ?>
	<p><?php the_tags(); ?></p>

<?php endwhile;?>

	<p><?php next_posts_link(); ?></p>
	<p><?php previous_posts_link(); ?></p>

<?php else : ?>

	<h1>Not Found</h1>
	<p>Silly monkey.</p>

<?php endif; ?>

What’s happening here is that we are running the loop only if we have posts, and only while the number of posts is less than or equal to the amount specified in the Admin area. Thus, limiting the number of posts is simply a matter of replacing “while(have_posts())” with something more restrictive.

There are probably a zillion ways of doing this, but the easiest that I have found is to simply setup a counter variable to keep track of each iteration of the loop. Once the counter variable reaches the specified value, the loop stops. Here’s the previous example loop showing the required modifications:

<?php $i = 1; while (have_posts() && $i < 6) : the_post(); ?>

	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
	<p>?php the_time(); ?></p>
	<?php the_content(); ?>
	<p><?php the_tags(); ?></p>

<?php $i++; endwhile; ?>

	<p><?php next_posts_link(); ?></p>
	<p><?php previous_posts_link(); ?></p>

<?php else : ?>

	<h1>Not Found</h1>
	<p>Silly monkey.</p>

<?php endif; ?>

Notice that in the first line we are initiating the counter variable “$i” and then setting its limit at “6”. We also add “$i++;” before the “endwhile” statement to increase the count by one for each iteration of the loop. The result of these modifications is that the loop will now run only five times (as specified in the first line) instead of ten times (as specified in the WordPress Admin).

Just keep in mind that this trick only works when you want to limit the total number of posts displayed on a particular page. Even so, it is still possible to avoid a plugin by setting your default post display to something high, like 20 or 30. Then, your blog displays the highest number of posts where you want them, and everything less is customized by limiting this default number with a couple lines of code. This technique could also be useful for designing themes that required a specific number of posts displayed in certain areas of the blog.

Method 2

Or, you could just use query_posts to create a custom query and specify the showposts parameter with a value of “5” (or whatever number you wish). Here is what our original loop example looks like using the query_posts function:

<?php query_posts('showposts=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>

	<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
	<p>?php the_time(); ?></p>
	<?php the_content(); ?>
	<p><?php the_tags(); ?></p>

<?php endwhile;?>

	<p><?php next_posts_link(); ?></p>
	<p><?php previous_posts_link(); ?></p>

<?php else : ?>

	<h1>Not Found</h1>
	<p>Silly monkey.</p>

<?php endif; wp_reset_query(); ?>

Notice the two additions to the original loop: in the first line, we added the query_posts function with the required posts_per_page parameter set to 5. Then, in the last line, we invoke the mystical powers of wp_reset_query to reset the original posts query. Resetting the query ensures that it’s available elsewhere in the template and is good practice when using query_posts.

Pros & Cons

The pros of using query_posts to modify the number of posts is that you can go either way — you can either decrease or increase the total number of posts displayed on any page — no plugins necessary.

The cons of using the query_posts method mainly involve the disruption of the original wp_query object normally used to communicate parameters involving page, category, search, and other properties to the SQL query2.

Avoiding this drama is one of the reasons why using a simple counter variable (as described in the first half of the post) is often the preferred way of going about it (multiple loops, anyone?).

Notes

  • 1 Editor’s note: 404 link removed.
  • 2 It is possible to include this default information in the query_posts query, but doing so is a bit beyond the scope of this article.

collect: https://digwp.com/2009/12/limit-posts-without-plugin/

12 months ago

Abdullah sk
I,m article writer, part time job in kokilbd
Like - Dislike Votes 0 - Rating 0 of 10

2 Comments

    abdullahsk
    July 26, 2019 at 2:53 am

    Reply

Leave a Reply

Your email address will not be published.


*