WordPress.org

Ready to get started?Download WordPress

Forums

How to check if an article has comments or not? (3 posts)

  1. lucas_man
    Member
    Posted 6 years ago #

    In every post/article I want to show the author name and date of the last comment added to that post/article. I use the code below to return this information.

    If there are one or more comments for a post/article it says:
    Last comment: Author on 4 Jun 2008 @ 10:11

    When there are no comments I would like to see:
    Last comment: -

    How do I do this? I tried to set comments_number = $zero with an is statement. Also tried to change comments_number('Last comment: - ', '<em>all of the code below</em> ', '<em>all of the code below</em> ' );

    <?php  // this shows the last comment author and date
    $last_comment = mysql_fetch_assoc(mysql_query("
    SELECT
    comment_ID,
    comment_author,
    comment_content,
    date_format( comment_date, '%e %b %Y @ %k:%i' ) AS formatted_date
    
    FROM
    wp_comments
    
    WHERE
    comment_post_ID = ".$id."
    
    ORDER BY
    comment_date DESC LIMIT 1"));
    ?>
    
    Last comment: <a href="<?php echo the_permalink().("#comment-"),$last_comment['comment_ID']; ?>" title="View comment"><?php echo $last_comment['comment_author']; ?></a> on <?php echo $last_comment['formatted_date']; ?>
  2. lucas_man
    Member
    Posted 6 years ago #

    When there are no comments WP returns
    Last comment: on

    How can I check if there are comments or not so I can change ON into - when there are not comments!

    So something like:

    <?php      if ($comments)
            echo("op");
    else
            echo("no");          ?>

    ?????

  3. lucas_man
    Member
    Posted 6 years ago #

    Already got it

    Changed the part that says 'on' into: <?php if ($comments); comments_number(' - ', ' on ', ' on ' ); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags