• 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!

Viewing 13 replies - 1 through 13 (of 13 total)
  • 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>
    Thread Starter paulak2

    (@paulak2)

    thanks much! i will try.

    pauljosephlp

    (@pauljosephlp)

    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

    2bak860

    (@2bak860)

    I would also like info on this!

    thanks in advance,

    R

    moshu

    (@moshu)

    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.

    2bak860

    (@2bak860)

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

    pauljosephlp

    (@pauljosephlp)

    Not working !!!
    I am getting a line error

    Any working ideas!!!

    Thanks for your effort guys!

    2bak860

    (@2bak860)

    Any ideas here?

    alkafy

    (@alkafy)

    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.

    pauljosephlp

    (@pauljosephlp)

    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

    pauljosephlp

    (@pauljosephlp)

    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

    alkafy

    (@alkafy)

    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:

    https://codex.www.ads-software.com/The_Loop

    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

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Display most recent comment for each post in index.php?’ is closed to new replies.