Support » Themes and Templates » Show comments on homepage posts without showing comment form

  • I would like to show comments on the posts of the homepage. The code below does it, but it also shows the comment form. I do not want the comment form shown on the homepage.

    <?php global $withcomments; $withcomments = 1; comments_template(); ?>

    I also want to limit the comments per post shown on the homepage to the latest 3, limiting the comments to 150 characters before showing ‘…’, if the person writes a long comment.

    Please help.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Michael


    Forum Moderator

    create a alternative comments template;

    start with a copy of comments.php, save for instance as comments-front.php; edit to remove the comment form code, and to limit to 3 comments.

    you might need to use a custom-callback for wp_list_comments() to get the shorter comment text ouput.

    (an example for a callback is in Twenty Eleven)

    Gosh, I think this could work. I have been going back and forth on the codex regarding comments, but didn’t find information on comments template. I’m going to try it out and let you know if it works.

    Thanks alchymyth.

            $comments = get_comments('number=3'); // Limits the number of comments shown to 3
                 foreach ($comments as $comment) : ?>

    I used this to limit comments to 3. It sorta works. The issue is that when there are 1 to 2 comments on the post, it retrieves another comment from another post. Is there something wrong with my code?

    Update: It doesn’t work. I propagated my posts with more comments. Each post is showing the same comments, when a comment exists on that post. Nothing shows on those that have no comments.



    Forum Moderator


    this is calling any latest three comments;

    possibly try instead:

    global $post;

    Thanks alchymyth. Here’s what happened.

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/accountname/public_html/dev/wp-content/themes/themeName/comments-box.php on line 27

    global $post;

    I suppose this is because of the apostrophes? So I switched to the below to avoid a parsing error.

    global $post;

    It gives the same result as you would get from omitting your portion of the code on post ID. Is there an alternative solution that doesn’t use get_comments. I don’t think it is working on the homepage loop. Thanks again for your input alchymyth.

    I think the portion that calls up post id needs an alternative.

    It seems that a call up of post id within the loop, actually gets the post id of the latest post in the homepage. It doesn’t get the post id of individual posts in the loop. Not sure how I can circumvent that.



    Forum Moderator

    my mistake – a single quote too much; should be:


    It works! Thanks alchymyth.

    As for the limiting of comment length, I used:

    <?php comment_excerpt( $comment_ID ); ?>

    The codex for this doesn’t allow arguments to define the excerpt length, which defaults at 20 words. How can I adjust it to show 10 words? Through wp_list_comments?



    Forum Moderator

    How can I adjust it to show 10 words?

    shortening the comments even more than 20 words might work with using a filter on the function;

    for example add this to functions.php of your theme:

    function even_shorter_comment_excerpt( $text ) {
    	$blah = explode(' ', $text);
    	if (count($blah) > 10) {
    		$k = 10;
    		$use_dotdotdot = 1;
    	} else {
    		$k = count($blah);
    		$use_dotdotdot = 0;
    	$excerpt = '';
    	for ($i=0; $i<$k; $i++) {
    		$excerpt .= $blah[$i] . ' ';
    	$excerpt .= ($use_dotdotdot) ? '...' : '';
    return $excerpt;

    – the code is basically a copy of the original core function get_comment_excerpt() from /wp-includes/comment-template.php line 407++)

    Oh. Okay. So, in the future I should take reference of filters within the core?

    Thanks again Alchymyth. I appreciate your help with this; especially because I’m not that good at wordpress codex functions and have close to no knowledge on php.

    I want comment excerpts by 300 characters not by words. Plz help me.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Show comments on homepage posts without showing comment form’ is closed to new replies.