Allow variable amount of comments before pagination

There is an option in the admin settings allowing you to define how many comments should be listed before creating a new page for the next ones. I would like to have the same number of comments for all my posts unless it belongs to one specific category (where I would like to set a different number of comments before pagination).

How can I do that?

Related posts

Leave a Reply

1 comment

  1. This have several components to it – what option itself holds and how value from it is stashed away and reused by various pieces of core code.

    I am not sure this is perfect, but my quick take would be:

    new Adjust_Comments_Per_Page( 10, 'years', 'category' );
    
    class Adjust_Comments_Per_Page {
    
        private $amount;
        private $term;
        private $taxonomy;
    
        /**
         * @param int    $amount
         * @param string $term
         * @param string $taxonomy
         */
        function __construct( $amount, $term, $taxonomy ) {
    
            $this->amount   = $amount;
            $this->term     = $term;
            $this->taxonomy = $taxonomy;
    
            add_action( 'template_redirect', array( $this, 'template_redirect' ) );
        }
    
        function template_redirect() {
    
            global $wp_query;
    
            if ( is_single() && has_term( $this->term, $this->taxonomy ) ) {
    
                $wp_query->set( 'comments_per_page', $this->amount );
                add_filter( 'pre_option_comments_per_page', array( $this, 'pre_option_comments_per_page' ) );
            }
        }
    
        /**
         * @return int
         */
        function pre_option_comments_per_page() {
    
            return $this->amount;
        }
    }