最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

customization - Is there a way to by-pass the pagination function on one custom template?

programmeradmin3浏览0评论

I'm working on a site where the client wants only the linked titles of all posts in a particular category on one page. That is, the query does not pick up the smaller number of posts that is defined in Settings from that one particular template.

I could do this if I by-pass the loop entirely, but is it possible within the normal loop?

I'm working on a site where the client wants only the linked titles of all posts in a particular category on one page. That is, the query does not pick up the smaller number of posts that is defined in Settings from that one particular template.

I could do this if I by-pass the loop entirely, but is it possible within the normal loop?

Share Improve this question asked May 17, 2020 at 21:15 Nora McDougall-CollinsNora McDougall-Collins 3952 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

The best way to do this is to use the pre_get_posts filter, which changes the main query. That way you're not running the default query plus a custom one on top of it. You'll have to determine the "if" conditions to only run on the query you want to affect:

<?php
function wpse_366882_get_all_posts($query) {
    // This will run for all main queries that are not in wp-admin.
    // You may want "is_archive()", "is_page('slug')" or some other condition.
    if ( ! is_admin() && $query->is_main_query() ) {
    // Setting to -1 shows all.
        $query->set( 'posts_per_page', -1 );
    }
}
add_action( 'pre_get_posts', 'wpse_366882_get_all_posts' );
?>

You can place this code in a custom plugin, or in a custom theme or child theme's functions.php file.

发布评论

评论列表(0)

  1. 暂无评论