How to check if post has previous_comments_link() and next_comments_link()

I'm using the following code for comments pagination (based on 2014 theme)

?php if( get_comment_pages_count()  1  get_option( 'page_comments' ) ) : ?
  ul class="pager"
    li class="previous"
      ?php previous_comments_link( __( 'larr; Older Comments', '' ) ); ?
    /li
    li class="next"
      ?php next_comments_link( __( 'Newer Comments rarr;', '' ) ); ?
    /li
  /ul
?php endif; ?

I noticed that if I'm on the first page, the .previous list item is still in the DOM. It's empty but still there.

li class="previous"
/li

I tried to find a proper function in the Functions Reference, but found nothing.

What I'm trying to do is to check conditionally if previous_comments_link() and next_comments_link() should be displayed. If not, then do not display the list item.

I don't know if this is a good idea (semantic) to remove whole list item.

Topic paginate-comments-links comments Wordpress

Category Web


Although this post is 4 years old, I want to add that while using

if(previous_comments_link()) :

and

if(next_comments_link()) :

will work, using

if(get_previous_comments_link()) :

and

if(get_next_comments_link()) :

is more efficient and closer to standard practice in the WP community since the get_ functions here won't run through the additional code that echoes the code. This is true for most (but not all) functions that start with "get_".


I should probably do

<?php if( previous_comments_link() ) : ?>
  <li class="previous">
    <?php previous_comments_link( __( '&larr; Older Comments', '' ) ); ?>
  </li>
<?php endif; ?>

<?php if( next_comments_link() ) : ?>
  <li class="next">
    <?php next_comments_link( __( '&larr; Newer Comments', '' ) ); ?>
  </li>
<?php endif; ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.