0

I want to limit the number of comments to show in wordpress template-page.php I didnt get any reasult in google searching. I want to use jquery for this problem but what i know is wordpress. Not full of php.

Ali Babae
  • 150
  • 1
  • 2
  • 11
  • I believe this is more of a database problem for which you can use the `LIMIT` clause. – Robert Jun 03 '17 at 17:46
  • You think jquery can not help me for limit the number of comments to '3' or '5' an have a button like 'More Comments' – Ali Babae Jun 03 '17 at 17:54
  • This can be done either in PHP or jQuery. The benefit to doing it in PHP (really via the SQL Query) is that this can reduce your lookup time and get results to you faster. Otherwise you will return all results to jQuery, which may take longer, and then have to remove or not show all the data. – Twisty Jun 03 '17 at 17:56
  • @AghaFree the comments come from a database if I am not mistaken. The 'load more' concept would be a combination of php and the database working together. PHP would send the result to your HTML page where you have jQuery. Here is a working example but with no database or php involved: https://stackoverflow.com/questions/17736786/jquery-load-first-3-elements-click-load-more-to-display-next-5-elements – Robert Jun 03 '17 at 17:58
  • @RobertRocha Thanks man! you gave me what i looking for. – Ali Babae Jun 04 '17 at 05:44

1 Answers1

0

I think you can use the WP_Comment_Query Class

Try the following. (this is a basic one though and you can customize this to your needs.)

Open your functions.php file and add the following function

function get_custom_comments(){

    $args = array(
   'number' => '1', // number of comments you want to show
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p>';
    }
} else {
    echo 'No comments found.';
}

Now in your single.php file, replace any comment template you have with the following

get_custom_comments();

PS: Add some CSS classes to make it look your original comment template.

Ayanize
  • 485
  • 1
  • 4
  • 21