Allow variable amount of comments before pagination

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;
    }
}

Leave a Comment