它有几个组件—选项本身是什么,以及它的价值是如何被各种核心代码隐藏和重用的。
我不确定这是否完美,但我的快速反应是:
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;
}
}