admin管理员组文章数量:1386662
I've had multiple requests from different clients to figure out a way to modify their blog (index.php) or Custom Post Type Archive page in order to show less posts from a certain category.
A good example would be a client that has a system of automatic posting from social media (Facebook, Twitter, Instagram), which posts to their WordPress blog in the "Social Media" category. If this client posts a lot to social media, and writes actual posts on their blog sparingly, the social media posts will dominate their blog (at least on the first page). In this case, the client needs a way to limit the posts in the "Social Media" category per page, on the blog.
I looked into using pre_get_posts
to modify the query, but can't seem to find any examples of how to limit the number of posts per page of a specific category, on the index.php page.
I was also considering making 2 separate queries (one for the "Social Media" category and another excluding this category) and combining them in a certain order. With this method, I was thinking that I could create a loop that combined the ID's of the 2 arrays (into 1), so that every fourth item would be from the "Social Media" category.
This sounds like a bit of hacking to me though, and I would like to find out if there is a more elegant/less messy way to get this done.
I've had multiple requests from different clients to figure out a way to modify their blog (index.php) or Custom Post Type Archive page in order to show less posts from a certain category.
A good example would be a client that has a system of automatic posting from social media (Facebook, Twitter, Instagram), which posts to their WordPress blog in the "Social Media" category. If this client posts a lot to social media, and writes actual posts on their blog sparingly, the social media posts will dominate their blog (at least on the first page). In this case, the client needs a way to limit the posts in the "Social Media" category per page, on the blog.
I looked into using pre_get_posts
to modify the query, but can't seem to find any examples of how to limit the number of posts per page of a specific category, on the index.php page.
I was also considering making 2 separate queries (one for the "Social Media" category and another excluding this category) and combining them in a certain order. With this method, I was thinking that I could create a loop that combined the ID's of the 2 arrays (into 1), so that every fourth item would be from the "Social Media" category.
This sounds like a bit of hacking to me though, and I would like to find out if there is a more elegant/less messy way to get this done.
Share Improve this question edited Jun 3, 2014 at 17:35 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Jun 3, 2014 at 16:22 seansean11seansean11 1011 bronze badge 4 |1 Answer
Reset to default 0The problem with this, as it often is, is not just homepage, but the continuous nature of pagination.
Let's say we start with 10 posts on home page and 5 of them social. Client says that they would like 2 less of social.
We can get rid of them easily enough, but now we need to "borrow" two more posts from second page. So we do that.
But next user clicks to second page. Instead of straightforward "next 10 posts" our logic is now "10 posts, after first 10, but displaying social posts we didn't display there and not displaying normal posts, which first page took from this one". Along these lines.
Next user clicks to page three... It compounds.
The only sane solution is two loops indeed. We leave normal posts and pagination up to main loop and we splice in anything extra in between (not necessarily modifying post array even, can just count and do something after every Nth post in loop), or displaying them separately as asides.
本文标签: wp queryLimit the number of posts from a specific category on indexphp
版权声明:本文标题:wp query - Limit the number of posts from a specific category on index.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744549580a2612102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
query_posts
should not be used. Check out this post to understand whyquery_posts
should not be used. And it not just us that say that, even the codex stated that it should not be used. Hope this helps you going forward – Pieter Goosen Commented Jun 3, 2014 at 17:48