Using WordPress ‘get_previous_comments_link()’ PHP function

The get_previous_comments_link WordPress PHP function retrieves the link to the previous comments page.

Usage

get_previous_comments_link($label);

Example:

echo get_previous_comments_link('View previous comments');

Parameters

  • $label string (Optional): Label for comments link text. Default: ”

More information

See WordPress Developer Resources: get_previous_comments_link

Examples

This code displays a link to the previous comments page with custom text.

if (get_previous_comments_link()) {
  echo '<div class="previous-comments">';
  echo get_previous_comments_link('View previous comments');
  echo '</div>';
}

Add a custom class to style the previous comments link.

if (get_previous_comments_link()) {
  echo '<div class="previous-comments">';
  echo get_previous_comments_link('<span class="custom-link">Previous Comments</span>');
  echo '</div>';
}

Conditional display based on the number of comments pages

Only show the previous comments link if there are more than two comments pages.

if (get_comment_pages_count() > 2 && get_previous_comments_link()) {
  echo '<div class="previous-comments">';
  echo get_previous_comments_link('Go to previous comments page');
  echo '</div>';
}

Add an icon to the previous comments link using Font Awesome.

if (get_previous_comments_link()) {
  echo '<div class="previous-comments">';
  echo get_previous_comments_link('<i class="fas fa-chevron-left"></i> Previous Comments');
  echo '</div>';
}

Create a custom HTML structure for the previous comments link.

if (get_previous_comments_link()) {
  echo '<div class="nav-previous">';
  echo '<span class="meta-nav">←</span> ';
  echo get_previous_comments_link('Older Comments');
  echo '</div>';
}