Forums

How to use the "existence of comments" in an IF statement (3 posts)

  1. yokima
    Member
    Posted 4 years ago #

    I wanted to use a

    php if (there are comments) { show this and that }

    syntax so I looked up comments.php and found this

    ?php if ($comments) : ?

    1. So I used this in archives.php (which is my substitue for index.php) within The Loop as

    php if ($comments) { blabla }

    but it returns false on every post, including those which have comments.

    2. I then tried pulling out the results from comments_popup_links

    php if (comments_popup_link('0','1','1')=='1') { echo('there are comments'); } ?

    and it didn't work. (the plugin just echoes the stuff on screen and doesn't pass on variables)

    So how can this be done? Help.

  2. Kafkaesqui
    Moderator
    Posted 4 years ago #

    $comments is not a global variable, but is created through the comments_template() function (just for the...comments template). So it won't be available otherwise.

    Try using get_comments_number(), which is the underlying function of comments_number():

    if(get_comments_number($post->ID))

  3. yokima
    Member
    Posted 4 years ago #

    It works! Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.