admin管理员组文章数量:1306871
I've got a custom loop function in my child theme, in the templates.php file to loop through specific post types ('listings') and then put it into an array to use into a part of my template.
Here's an example of the code
public function loop()
{
global $wp_query;
$featured = $regular = array();
foreach ($wp_query->posts as $count => $post)
if (has_term('featured', 'listings_tag', $post->ID))
$featured[] = $post;
else
$regular[] = $post;
$posts = array_merge($regular, $featured);
$posts = array_reverse($posts);
include('templates/loop.php');
}
So this is great, however, it paginates after every 11 posts. Instead I would like it to display everything instead.
I tried by adding a custom action in my functions.php, making use of the 'pre_get_posts' hook. However it doesn't have any effect on my code.
function show_all($query){
if (is_post_type_archive('listings') && $query->is_main_query()) {
$query->set('nopaging', True);
}
}
add_action('pre_get_posts', 'show_all');
Any suggestions?
I've got a custom loop function in my child theme, in the templates.php file to loop through specific post types ('listings') and then put it into an array to use into a part of my template.
Here's an example of the code
public function loop()
{
global $wp_query;
$featured = $regular = array();
foreach ($wp_query->posts as $count => $post)
if (has_term('featured', 'listings_tag', $post->ID))
$featured[] = $post;
else
$regular[] = $post;
$posts = array_merge($regular, $featured);
$posts = array_reverse($posts);
include('templates/loop.php');
}
So this is great, however, it paginates after every 11 posts. Instead I would like it to display everything instead.
I tried by adding a custom action in my functions.php, making use of the 'pre_get_posts' hook. However it doesn't have any effect on my code.
function show_all($query){
if (is_post_type_archive('listings') && $query->is_main_query()) {
$query->set('nopaging', True);
}
}
add_action('pre_get_posts', 'show_all');
Any suggestions?
Share Improve this question asked Jan 20, 2021 at 20:38 Ronald LangeveldRonald Langeveld 1052 bronze badges 5 |1 Answer
Reset to default 1nopaging
does not fetch all posts, it just tells WordPress not to bother figuring out stuff like how many pages there are.
What you actually wanted was not to disable pagination, that's just a solution to your problem that you asked about. You should ask about your problem instead, aka how to show all posts at once.
Showing all posts at once can be dangerous, so instead, set a limit that's very high, e.g:
$query->set('posts_per_page', 500 );
Anything more than 500 posts will probably cause problems, or be extremely slow, as well as being extremely long winded. The idea being that you specify a number you never expect to reach. That way you know for a fact that the worst case scenario can't happen. Why trust that there won't be a broken plugin or a client who does something by accident when you can know for a fact the page won't break?
本文标签: loopQuery Nopaging action not having effect
版权声明:本文标题:loop - Query Nopaging action not having effect 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741816333a2399102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$query->is_post_type_archive.....
. Otherwise,nopaging
doesn't do what you think it does. Likewise it looks like you've re-implemented sticky posts – Tom J Nowell ♦ Commented Jan 20, 2021 at 21:14