0

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.

2 Answers 2

0

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; ?>
0

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_".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.