Sort post comments from newest to oldest in pagination
I searched for a whole day before asking this question, but couldn’t find anything to help me.
How can I sort the comments on all my posts from most recent to oldest? I made a custom theme and that’s why I’m asking.
This is my setting in Settings > Discussion:
And this is my code in comments.php:
<?php if ( have_comments() ) : ?>
<!-- Numero commenti -->
<span class="numero-commenti">
<?php
if(!is_page('testimonianze')){
printf( _nx( '<span class="numero">1</span> Commento <hr class="linea"><a href="#respond">Lascia un commento</a>', '<span class="numero">%1$s</span>Commenti<hr class="linea"><a href="#respond">Lascia un commento</a>', get_comments_number(), 'comments title', 'soswp' ), number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
} else {
/*
printf( _nx( '<span class="numero">1</span>Recensione<hr class="linea"><a href="#respond">Lascia una recensione</a>', '<span class="numero">%1$s</span>Recensioni<hr class="linea"><a href="#respond">Lascia una recensione</a>', get_comments_number(), 'comments title', 'soswp' ), number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );*/
}
?>
</span>
<!-- List comments -->
<ol class="lista-commenti">
<?php wp_list_comments( array(
'max_depth' => 2,
'per_page' => 5,
'style' => 'ol',
'reverse_top_level' => true,
//'short_ping' => true,
'avatar_size' => 125,
'type' => 'comment',
'callback' => 'soswp_comment',
));?>
</ol>
<?php $page = ( get_query_var('paged') ) ? get_query_var('paged') : 1; ?>
<div class="navigation">
<?php paginate_comments_links(array(
'add_fragment' => '#commenti',
'current' => $page,
)); ?>
</div>
<!-- Mostra commenti chiusi -->
<?php if ( ! comments_open() && get_comments_number() ) : ?>
<p class="no-comments"><?php _e( 'I commenti per questo articolo sono stati chiusi. Se hai bisogno di aiuto contattami tramite la chat in basso a destra.' , 'soswp' ); ?></p>
<?php endif; ?>
<?php endif; ?>
<div class="form-commenti">
<?php comment_form(); ?>
</div>
Here is a page from the site where you can see that the first page loads fine, but then when you switch to another page the comments are shown from most recent to oldest on the single page (correct), but from oldest to newest on the pages:
https://sos-wp.it/creare-siti-web/blog/100-argomenti-per-creare-un-blog/#commenti
How do I fix this?
Leave an answer