WordPress.org

Support

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

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

    @alchymyth

    Forum Moderator

    create a alternative comments template;

    http://codex.wordpress.org/Function_Reference/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.
    http://codex.wordpress.org/Function_Reference/wp_list_comments

    (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.

    <?php
            $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.

    Michael

    @alchymyth

    Forum Moderator

    get_comments('number=3');

    this is calling any latest three comments;

    possibly try instead:

    global $post;
    get_comments('number=3&post_id='.$post->ID');

    http://codex.wordpress.org/Function_Reference/get_comments

    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;
    get_comments('number=3&post_id='.$post->ID');

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

    global $post;
    get_comments("number=3&post_id='.$post->ID'");

    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.

    Michael

    @alchymyth

    Forum Moderator

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

    get_comments('number=3&post_id='.$post->ID);

    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?

    Michael

    @alchymyth

    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:

    add_filter('comment_excerpt','even_shorter_comment_excerpt');
    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;
    }

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

    http://codex.wordpress.org/Plugin_API#Filters

    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.
Skip to toolbar