WordPress.org

Ready to get started?Download WordPress

Forums

Allow variable amount of comments before pagination (5 posts)

  1. baylock
    Member
    Posted 1 year ago #

    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 to be seen. The thing is 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).

  2. baylock
    Member
    Posted 1 year ago #

    Would someone have the kindness to answer this?
    It doesn't have to solve the problem. Even a workaround would do cause I'm really in trouble here.

  3. bcworkz
    Member
    Posted 1 year ago #

    Hmmm. Maybe you need to create a custom version of get_comment_pages_count() and put the definition on the functions.php of a simple child theme. Then copy your main theme's comments.php to the child folder and edit it to call your custom version instead of get_comment_pages_count().

    The custom version would change the $per_page value based on the category of the post. The call to wp_list_comments also on the template needs to be modified based on category as well. There may be some navigation issues with this that need tweaking as well.

  4. baylock
    Member
    Posted 1 year ago #

    Mmm.. thank you very much bcworkz.
    In the meantime, would you know a quick way to just completely disable pagination for one category only?
    I plan to fix this for the long term (with your solution perhaps) but right now, the interface is quite ugly and I had to deactivate pagination everywhere, even when I have a half a hundred comments on a post.

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Here is a (bit of a hacky) way do do this. Be sure you only show comments on single posts or/and single Pages or/and singe attachment pages (usually default). Put this in your theme's functions.php:

    function comment_pagination_query( $query ) {
    
      $category = 25; // category ID
      $number_of_comments_for_category = 5; // comments_per_page for category 25
      $normal_number_of_comments = 3; // comments_per_page for all other categories
    
      if (!is_admin() && $query->is_main_query()){
    
        if(is_singular()) { // single post, page, attachment
          if(isset($query->query_vars['name']) && $query->query_vars['name']) {
            $the_post = get_posts('posts_per_page=1&name='.$query->query_vars['name']);
            if(isset($the_post[0]->ID) && $the_post[0]->ID) {
              if(has_term( $category, 'category', $the_post[0]->ID)) {
    	          update_option('comments_per_page', $number_of_comments_for_category);
              } else {
    	          update_option('comments_per_page', $normal_number_of_comments);
              }
            }
          }
        }
      }
    }
    add_action( 'pre_get_posts', 'comment_pagination_query' );

    Change the variables to the category and number of posts you want:

    $category = 25; // category ID
      $number_of_comments_for_category = 5; // comments_per_page for category 25
      $normal_number_of_comments = 3; // comments_per_page for all other categories

Topic Closed

This topic has been closed to new replies.

About this Topic