• Resolved Andry

    (@blackstar1991)


    I have I test site with theme/twentytwenty. There is a need to implement a toggle button, that when it is pressed, the comments that were shown to the post, with pagination (from old to new) after its pressing were displayed from new to old (with the same pagination). In the documentation of?wp_list_comments()?there is a description of the ‘reverse_top_level’ parameter which is responsible for that. But in my case something goes wrong. Here is my code for ?\wp-content\themes\twentytwenty\template-parts\custom-comments-template.php

    <?php if (post_password_required()) {
        return;
    }
    $post_id = get_the_ID();
    $per_page = 4;
    $comments_count = get_comments_number($post_id);
    
    
    
    if (isset($_GET['cpage']) && is_numeric($_GET['cpage'])) {
        $page = (int)$_GET['cpage'];
    }
    $page = get_query_var('cpage');
    $page = is_numeric($page) ? (int)$page : 1;
    
    
    
    
    $GET_value_filter = filter_input(INPUT_GET, 'filter', FILTER_SANITIZE_ENCODED);
    
    ?>
    
    <a href="/test/?filter=newest">NEWS COMMENTS</a>
    
    <div id="comments" class="company-rewiew-list">
        <?php
            var_dump($page);
    
            if (empty($GET_value_filter)) {
                function custom_default_comments_page1($value)
                {
                    return 'oldest';
                }
    
                add_filter('pre_option_default_comments_page', 'custom_default_comments_page1');
                wp_list_comments(array(
                    'type' => 'comment',
                    'reverse_top_level' => false,
                    'reverse_children' => false,
                    'callback' => 'reviews_theme_comment',
                    'per_page' => $per_page,
                    'number' => $per_page,
                    'cpage' => $page,
                ));
    
    
            } elseif ($GET_value_filter == 'newest') {
    
                echo '<h1>We need to show newest order comments</h1>';
    
                $comment_count = wp_count_comments($post_id);
                $comments_count = $comment_count->approved;
    
                $offset = ($page - 1) * $per_page;
    
                $args = array(
                    'type' => 'comment',
                    'status' => 'approve',
                    'post_id' => $post_id,
                    'order' => 'DESC', // Sort comments in descending order (newest to oldest)
                    'offset' => $offset,
                    'number' => $per_page,
                );
    
                $comments = get_comments($args);
    
                wp_list_comments(array( // Remove reverse_top_level parameter
                    'type' => 'comment',
                    'callback' => 'reviews_theme_comment',
                    'comments' => $comments,
                    'total' => $comments_count,
                    'per_page' => $per_page,
                ));
            }
        ?>
    </div>
    
    <?php
    function reviews_theme_comment($comment, $args, $depth)
    {
        $comment_ID = intval($comment->comment_ID);
        if (!$comment->comment_parent) {
    
            echo '<div class="rewiew-card">';
            ?>
    
            <div id="comment-<?php comment_ID() ?>"
                 itemprop="review" itemscope itemtype="https://schema.org/Review">
                    <div class="review_content__text"><?php comment_text(); ?></div>
            </div>
    
            <?php
        } else {
    
            ?>
            <li id="comment-<?php comment_ID() ?>" class="rewiew-card-item _reply">
                    <div class="review_content__text"><?php comment_text(); ?></div>
                </div>
            </li>
            <?php
    
        }
    
        if (($depth == 1)) {
            echo '</div>'; 
        }
    
        ?>
    
    <?php } ?>
    
    
    <?php
    if (($per_page > 1) && ($comments_count > $per_page)) {
        include locate_template('template-parts/custom-comments-pagination.php');
    }
    ?>

    \wp-content\themes\twentytwenty\template-parts\custom-comments-pagination.php

    <?php
    
    $args = array(
        'post_id' => get_the_ID(),
        'parent' => 0,
        'count' => true,
    );
    $top_level_comments_count = get_comments($args);
    $max_pages = ceil($top_level_comments_count / $per_page);
    
    $args = array(
        'screen_reader_text' => __('Comments navigation'),
        'aria_label' => __('Comments'),
        'class' => 'comments-pagination',
        'format' => '',
        'total' => $max_pages,
        'current' => $page,
        'prev_text' => '?',
        'next_text' => '?',
        'type' => 'plain',
        'echo' => false,
    );
    
    $pagination = get_the_comments_pagination($args);
    $pagination = str_replace('#comments', '', $pagination);
    ?>
    <div class="company-rewiew-pagination">
        <?php echo $pagination; ?>
    </div>
    

    My comments.php

    <?php
    /**
     * The template for displaying comments
     *
     * @package Caards
     */
    
    ?>
    
    <?php do_action('csco_comments_before'); ?>
    
    <?php
    $style = 'cs-entry__comments-collapse';
    
    if ('page' === get_post_type()) {
        if (get_theme_mod('page_comments_simple', false)) {
            $style = 'cs-entry-comments-simple';
        }
    } else {
        if (get_theme_mod('post_comments_simple', false)) {
            $style = 'cs-entry-comments-simple';
        }
    }
    
    if (get_option('comment_registration') && !is_user_logged_in()) {
        $style = 'cs-entry-comments-simple';
    }
    
    $comments_id = 'cs-entry-comments-simple' === $style ? 'comments' : 'comments-hidden';
    ?>
    
    <div class="cs-entry__comments <?php echo esc_attr($style); ?> comments_posts" id="<?php echo esc_attr($comments_id); ?>">
    
        <?php if (have_comments()) { ?>
    
            <?php
            $comments_number = get_comments_number();
    
            if (1 === $comments_number) {
                $section_heading = esc_html_e('One comment', 'caards');
            } else {
                /* translators: 1: number of comments */
                $section_heading = esc_html__('Comments', 'caards') . ' <span>' . esc_html($comments_number) . '</span>';
            }
    
            if (function_exists('csco_layout_heading')) {
                $section_heading = esc_html__('Comments', 'caards') . ' <span>' . esc_html($comments_number) . '</span>';
                csco_layout_heading($section_heading);
            } else {
                // Fallback in case the function is not defined
                echo '<h5>' . esc_html__('Comments', 'caards') . ' <span>' . esc_html($comments_number) . '</span></h5>';
            }
            ?>
    
            <?php the_comments_navigation(); ?>
    
            <ol class="comment-list">
                <?php
                wp_list_comments(
                    array(
                        'style' => 'ol',
                        'short_ping' => true,
                        'avatar_size' => 60,
                        'callback' => 'custom_comment_callback',
                    )
                );
                ?>
            </ol>
    
            <?php the_comments_navigation(); ?>
    
        <?php } ?>
    
        <?php
        // If comments are closed and there are comments, let's leave a little note, shall we?
        if (!comments_open() && get_comments_number() && post_type_supports(get_post_type(), 'comments')) {
            ?>
            <p class="no-comments"><?php esc_html_e('Comments are closed.', 'caards'); ?></p>
        <?php } ?>
    
    </div>
    
    <?php
    function custom_comment_callback($comment, $args, $depth)
    {
        echo '<li id="comment-' . get_comment_ID() . '"';
        comment_class();
        echo '>';
    
        if ( $comment->comment_approved == '0' ) {
            echo '<em class="comment-awaiting-moderation comments_posts_moderation">Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.</em>';
        }
    
        echo '<article id="div-comment-' . get_comment_ID() . '" class="comment-body">';
        echo '<footer class="comment-meta">';
        echo '<div class="comment-author vcard">';
    
        if ( 0 != $args['avatar_size'] ) {
            echo get_avatar( $comment, $args['avatar_size'] );
        }
    
        printf( ' <b class="fn">%s</b>', get_comment_author_link() );
        echo '<span class="says">';
        esc_html_e( 'says:', 'caards' );
        echo '</span>';
    
        echo '</div><!-- .comment-author -->';
        echo '<div class="comment-metadata">';
        echo '<a href="' . esc_url( get_comment_link( $comment, $args ) ) . '">';
        echo '<time datetime="' . get_comment_time( 'c' ) . '">' . get_comment_date() . ' at ' . get_comment_time() . '</time>';
        echo '</a>';
        echo '</div><!-- .comment-metadata -->';
        echo '</footer><!-- .comment-meta -->';
        echo '<div class="comment-content">';
        comment_text();
        echo '</div><!-- .comment-content -->';
        echo '<div class="reply">';
    
        comment_reply_link( array_merge( $args, array( 'reply_text' => esc_html( 'Reply' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) );
    
        echo '</div><!-- .reply -->';
        echo '</article><!-- .comment-body -->';
        echo '</li>';
    }
    ?>
    
    <?php comment_form(
        array(
            'title_reply_before' => csco_layout_heading(null, 'h5', 'before', false),
            'title_reply_after' => csco_layout_heading(null, 'h5', 'after', false),
        )
    ); ?>
    
    <?php if ('cs-entry__comments-collapse' === $style) : ?>
        <div class="cs-entry__comments-show" id="comments">
            <button><?php esc_html_e('View Comments', 'caards'); ?> (<?php echo intval(get_comments_number()); ?>)</button>
        </div>
    <?php endif; ?>
    
    <?php do_action('csco_comments_after'); ?>
    

    But it work incorrect for older comments.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator jordesign

    (@jordesign)

    Hi @blackstar1991,

    Is this something that only happens when you try it with TwentyTwenty? Or does it happen with other themes as well?

    I’ll be honest – I don’t know the answer to how to get that working. I’d recommend asking in one of the more general WordPress forums, which often have a lot more eyes on them (and you might find someone who does know).

    I’d suggest trying here:
    https://www.ads-software.com/support/forum/wp-advanced/?view=all

    Thread Starter Andry

    (@blackstar1991)

    Thanks, I see this problem in all templates. I showed this on Twenty Twenty because it’s a simple template to display by default. I think that it can be WP core problem…

    I’ll try to ask on this forum thread, thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display comments in reverse order with pagination?’ is closed to new replies.