• Resolved Alkorr

    (@alkorr)


    Hi! When on a single post, I would to show the latest comment right below the title, and then at the end of the post, all the comments for this post.

    I know how to show all the comments to this post using <?php comments_template(); ?> but I’m unable to show the latest post on top of my post. I tried to use <?php comment_excerpt(); ?> but I didn’t find a way to make it work.

    Any help is welcome, thanks! šŸ™‚

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter Alkorr

    (@alkorr)

    I checked the WP Forums again and couldn’t find any information about what I’m looking for. I guess it is not a very common request…

    If someone thinks of something, let me know šŸ˜‰

    have you studied how to use ‘get_comments()’ ?

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

    Thread Starter Alkorr

    (@alkorr)

    Hi alchymyth! As a matter of fact I did but I couldn’t find a way to show the latest post for the current post. If I use the post ID, then it is workong only for this post ID, not any other.

    I’m still trying to make it work using my poor PHP knowledge. I always think what I’m looking for requieres ‘simple’ code but it apparently doesn’t šŸ™‚

    Thread Starter Alkorr

    (@alkorr)

    For info, I have tried to use the code from this thread but they don’t work for me:

    http://wordpress.org/support/topic/passing-parameter-to-get_comments-function

    the post ID of the current post is usually $post->ID;

    example:

    <?php $args = array(
        'number' => 1,
        'post_id' => $post->ID,
        'status' => 'approve'
    );
    $latest_comment = get_comments( $args );
    if( $latest_comment ) foreach( $latest_comment as $comment ) {
    /*whatever you want to show from the comment, for instance*/ ?>
    latest comment: <?php echo $comment->comment_date; ?>
     by: <?php echo $comment->comment_author; ?>
    <p><?php echo $comment->comment_content; ?></p>
    <?php } ?>

    (untested)

    edit:
    after reading your last reply:
    if $post->ID does not work, try get_the_ID() instead.

    (details might depend on your existing template, single.php (?)

    Thread Starter Alkorr

    (@alkorr)

    Thanks alchymyth, your code works perfectly! šŸ™‚

    Last point: add a link on comment to read full comment.

    ID).”#comment-” . $comment->comment_ID . “\” title=\”Read comment\”><?php echo $comment->comment_content; ?>

    Doesn’t seem to work since I have to define first $comment->ID I guess.

    Also, if there is no comment for the post, I tried:

    <? else {
    echo “No comment”;
    } ?>

    But it doesn’t seem to work…

    I’m a persevering person even if I don’t always understand what I’m doing, at least I’m trying šŸ™‚

    Thread Starter Alkorr

    (@alkorr)

    Ok, forget about the ‘No comment’ thing, I will just show nothing if there is no comment.

    I’m still ‘working’ on the link to the full comment!

    could you show the full code of the section, as it is now?
    (if it is longer than 10 lines, please use the pastebin http://codex.wordpress.org/Forum_Welcome#Posting_Code )

    do you just want to show a comment excerpt, which is linked to the full comment?

    Thread Starter Alkorr

    (@alkorr)

    Sure, here my code to show the latest comment:

    <?php $args = array(
        'number' => 1,
        'post_id' => $post->ID,
        'status' => 'approve'
    );
    $latest_comment = get_comments( $args );
    if( $latest_comment ) foreach( $latest_comment as $comment ) { ?>
    Latest comment:<?php echo $comment->comment_author; ?>: <?php echo wp_html_excerpt( $comment->comment_content, 100 ); ?>..."
    <?php } ?>

    As you can see, I managed to trim the excerpt šŸ™‚

    Now I indeed want to link the comment excerpt to the full comment, that’s it!

    ...
    $latest_comment = get_comments( $args );
    if( $latest_comment ) foreach( $latest_comment as $comment ) { ?>
    Latest comment: <?php echo $comment->comment_author; ?>: <a href="<?php the_permalink(); ?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo wp_html_excerpt( $comment->comment_content, 100 ); ?>...</a>"
    <?php } ?>
    Thread Starter Alkorr

    (@alkorr)

    Thanks you so much alchymyth! That’s exactly what I was *trying* to do.

    Once again, you made my day šŸ™‚

    you are welcome –

    could you please set this topic to ‘resolved’? (there is is box on the right somewhere…)
    thanks šŸ˜‰

    this will help others to see that the questions was answered, and help helpers to avoid looking at it.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Show latest comment on single post’ is closed to new replies.