Is there a way to override how wordpress displays a number of posts on a post listing page?
I need to limit the number of posts on a page by month instead of limiting it to a number of posts per page, so each previous and next goes back and forth to the previous or next month where there are posts
I did try this method, which seemed to be nearly there.
Making next_posts_link(); return posts by month
But page 2 and onwards returned no results. It also didn’t take in to account where you are in the site, such as category/tag etc
Thanks
If I understand correctly you want to display all month of June on one page? This would require overiding the global $per_page setting it to a high number and also you would need to filter
pre_get_posts
and adjust the SQL query string to return results by date. The post you linked has the answer. Perhaps displaying your code will allow us to see a problem.If I understand your question you want that for all archives (category, tag, author…) you want to display on 1st page the posts related to the archive but only posted in the current month.
Then you want have link ‘Next Page’ an ‘Previous Page’ that shows the posts for the next and the previous month respectively.
The ansqwer you linked gives a grat hel to you, you have to optimize it to run on every archive hooking the
pre_get_post
action.My suggestion is to use the
m
parameter to the required a month, and then setposts_per_page = -1
.This will force to show all the posts for the required month, and using navigation you can browse by month.
Then you need some logic to make the first page (when no
page
is in the url) show all the pages for the month of the last post published.After that you can create a couple of function that helps you to output the navigation urls.
I’ve created a simple plugin that handle what I said:
Comments in code will help you to better understand what plugin does.
Save the plugin in a file under plugin directory and activate it.
After that, in your template files that handle archives, like ‘home.php’, ‘taxonomy.php’, ‘category.php’ and so on put this code to show navigation links:
In your index.php you can wrap it in a if statement that prevent to show links in not supported pages like singular views or date based archives:
Note that the pagination links are generated by plugin according to your permalink structure. In no pretty permalinks are active the links will use url variables.