Forums

[resolved] Show latest comment on single post (13 posts)

  1. Alkorr
    Member
    Posted 10 months ago #

    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! :)

  2. Alkorr
    Member
    Posted 10 months ago #

    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 ;)

  3. alchymyth
    The Sweeper
    Posted 10 months ago #

    have you studied how to use 'get_comments()' ?

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

  4. Alkorr
    Member
    Posted 10 months ago #

    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 :)

  5. Alkorr
    Member
    Posted 10 months ago #

    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

  6. alchymyth
    The Sweeper
    Posted 10 months ago #

    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 (?)

  7. Alkorr
    Member
    Posted 10 months ago #

    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 :)

  8. Alkorr
    Member
    Posted 10 months ago #

    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!

  9. alchymyth
    The Sweeper
    Posted 10 months ago #

    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?

  10. Alkorr
    Member
    Posted 10 months ago #

    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!

  11. alchymyth
    The Sweeper
    Posted 10 months ago #

    ...
    $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 } ?>
  12. Alkorr
    Member
    Posted 10 months ago #

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

    Once again, you made my day :)

  13. alchymyth
    The Sweeper
    Posted 10 months ago #

    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.

Reply

You must log in to post.

About this Topic