6

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?

1
  • I asked this question many times and never got an answer. Is it impossible to achieve or am I not clear about the issue? Nov 20, 2012 at 6:59

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.