WordPress.org

Ready to get started?Download WordPress

Forums

Display most recent comment for each post in index.php? (14 posts)

  1. paulak2
    Member
    Posted 6 years ago #

    Hi, I've found many plugins/php code to display recent comments on the sidebar, but I'm just looking to place the latest comment for a post right below the actual post in the index.php page. I've been searching for a way to do this, but with no luck.

    I tried using this tag in the index.php:

    <?php $withcomments = true; comments_template();?>

    but it makes every comment related to the post appear and the comment form, which I don't want.

    Any help is appreciated. Thanks!

  2. haochi
    Member
    Posted 6 years ago #

    Doesn't look like WordPress has such function, so you need to roll out your own.

    <?php
    $haochi = mysql_fetch_assoc(mysql_query("SELECT comment_ID, comment_author, comment_content, comment_date FROM wp_comments WHERE comment_post_ID = ".$id." ORDER BY comment_date DESC LIMIT 1"));
    ?>

    and to use it:

    "<?php echo $haochi['comment_content']; ?>" -
    <?php echo $haochi['comment_author']; ?> on
    <?php echo date("m/j/y", $haochi['comment_date']; ?>
    <a>#comment-<?php echo $haochi['comment_ID']; ?>">#</a>
  3. paulak2
    Member
    Posted 6 years ago #

    thanks much! i will try.

  4. pauljoseph.lp
    Member
    Posted 5 years ago #

    I am a starter and could you give me information on how to include

    <?php
    $haochi = mysql_fetch_assoc(mysql_query("SELECT comment_ID, comment_author, comment_content, comment_date FROM wp_comments WHERE comment_post_ID = ".$id." ORDER BY comment_date DESC LIMIT 1"));
    ?>

    this above code and this one too

    "<?php echo $haochi['comment_content']; ?>" -
    <?php echo $haochi['comment_author']; ?> on
    <?php echo date("m/j/y", $haochi['comment_date']; ?>
    <a>#comment-<?php echo $haochi['comment_ID']; ?>">#</a>

    Thanks

  5. 2bak860
    Member
    Posted 5 years ago #

    I would also like info on this!

    thanks in advance,

    R

  6. moshu
    Member
    Posted 5 years ago #

    I guess the first snippet goes into the functions.php of the theme and the second one into the index, where you want them to appear, just below the posts.

  7. 2bak860
    Member
    Posted 5 years ago #

    I thought so to, but no dice. Any ideas?

  8. pauljoseph.lp
    Member
    Posted 5 years ago #

    Not working !!!
    I am getting a line error

    Any working ideas!!!

    Thanks for your effort guys!

  9. 2bak860
    Member
    Posted 5 years ago #

    Any ideas here?

  10. alkafy
    Member
    Posted 5 years ago #

    Here's my very basic implementation on WordPress 2.3, before I added any markup:

    <?php $posts = get_posts("category=22&numberposts=1");
    if($posts) : foreach($posts as $post) : setup_postdata($post);

    $haochi = mysql_fetch_assoc(mysql_query("SELECT comment_ID, comment_author, comment_content, comment_date FROM wp_comments WHERE comment_post_ID = '".$post->ID."' AND comment_approved = '1' ORDER BY comment_date DESC LIMIT 1"));

    echo $haochi['comment_author'];
    echo $haochi['comment_content'];

    endforeach; endif; ?>

    The only difference between mine and haochi's original is the "AND comment_approved = '1'" because it was displaying spam caught by Akismet and unapproved comments.

  11. pauljoseph.lp
    Member
    Posted 5 years ago #

    Hello Alkafy,
    Could you tell me how to add the above code in which file and which line.

    I am illierate in wordpress code editing.

    Thanks for your effort, really appreciate it

    Paul

  12. pauljoseph.lp
    Member
    Posted 5 years ago #

    I tried to make it work and it succeded but small problem
    It is showing only comments of 1 post - the last post in that category.

    How can i show the last comment for every post

    Paul

  13. alkafy
    Member
    Posted 5 years ago #

    Hi Paul,

    To get comments from every category, remove "category=22&numberposts=1" from line 1. To change the number of comments you get, change "LIMIT 1" to the number you'd like to display. For example, ten comments would be "LIMIT 10".

    To display the last comment of a specific post, put the code in the loop that produces the post, but remove

    $posts = get_posts("category=22&numberposts=1");
    if($posts) : foreach($posts as $post) : setup_postdata($post);

    and

    endforeach; endif;

    The $haochi code should be fine as long as you're in a loop. If you're unsure of what the loop looks like, check here:

    http://codex.wordpress.org/The_Loop

  14. lobos55
    Member
    Posted 5 years ago #

    Alkafy,
    Great code. Just one problem, change of the limit does not work, I just get the last comment.
    Here my code:

    <?php $posts = get_posts("p=3666");
    if($posts) : foreach($posts as $post) : setup_postdata($post);$haochi = mysql_fetch_assoc(mysql_query("SELECT comment_ID, comment_author, comment_content, comment_date FROM wp_comments WHERE comment_post_ID = '".$post->ID."' AND comment_approved = '1' ORDER BY comment_date DESC LIMIT 10"));

    echo "<p><b>\n";
    echo $haochi['comment_author'];
    echo "</b>
    \n";
    echo $haochi['comment_content'];
    echo "
    \n";
    echo $haochi['comment_date'];
    echo "</p>\n";
    endforeach; endif; ?>

    Any idea why I don't get more comments?
    Thank you for asnwering

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.