• Resolved r3ndy

    (@r3ndy)


    Hi GeneratePress team,

    I moved the comment form to the top using hooks and custom CSS. Everything works correctly, but the “Older Comments” and “Newer Comments” navigation is displayed twice.

    I only want a single comment navigation section without duplicates.

    Here is the customization I currently use:

    <?php
    /**
    add_action( 'wp', function() {
    // 1. Hapus form dari posisi default (bawah)
    remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15 );

    // 2. Pasang kembali di posisi atas (sebelum container komentar)
    // Kita panggil fungsi asli GP tapi biarkan sistem yang kirim argumennya
    add_action( 'generate_before_comments_container', 'generate_do_comments_template' );
    });

    /**
    add_action( 'wp_head', function() {
    ?>
    <style>

    #comments {
    display: flex !important;
    flex-direction: column !important;
    }
    #respond {
    order: -1 !important;
    margin-bottom: 30px !important;
    border-bottom: 2px solid #eeeeee;
    padding-bottom: 20px !important;
    }
    .comments-title, .comment-list, .comment-navigation {
    order: 1 !important;
    }

    .comment-form {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    }
    .comment-form-comment, .custom-comment-notes, .form-submit, .comment-form-cookies-consent {
    grid-column: span 2;
    }
    @media (max-width: 768px) {
    .comment-form { grid-template-columns: 1fr; }
    .comment-form-comment, .comment-form-author, .comment-form-email { grid-column: span 1; }
    }
    </style>
    <?php
    });

    /**
    add_filter( 'comment_form_defaults', function( $defaults ) {
    $defaults['comment_notes_before'] = '
    <div class="custom-comment-notes" style="margin-bottom:15px; font-size:14px; color:#aaa;">
    <p>Gunakan email valid agar kamu mendapat notifikasi balasan.</p>
    <p>Kirim <a href="https://prnt.sc/" target="_blank" rel="nofollow noopener noreferrer" style="color:#0033ff;">screenshot</a> komentar kamu di sini.</p>
    </div>';
    return $defaults;
    });

    /**
    add_filter( 'comment_form_fields', function( $fields ) {
    if(isset($fields['comment'])) {
    $comment_field = $fields['comment'];
    unset( $fields['comment'] );
    $fields['comment'] = $comment_field;
    }
    return $fields;
    });
    ?>

    I also use custom CSS to reorder the comment form visually.
    Screenshot: https://prnt.sc/UB-r80GHammo

    Could you please check the correct method to prevent duplicate comment navigation in this setup?

    Thank you.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • George

    (@quantum_leap)

    Hi there,

    This is GP’s standard comments markup, not your PHP. comments.php outputs .comment-navigation both before (#comment-nav-above) and after (#comment-nav-below) .comment-list. Normally the list sits between them so you don’t notice, but this rule in your CSS:

    
    .comments-title, .comment-list, .comment-navigation {
        order: 1 !important;
    }
    

    …gives all four elements (title, list, and both navs) the same order, so they collapse together.

    Replace that single rule with:

    
    .comments-title {
        order: 1 !important;
    }
    #comment-nav-above {
        order: 2 !important;
    }
    .comment-list {
        order: 3 !important;
    }
    #comment-nav-below {
        order: 4 !important;
    }
    

    Everything else in your CSS can stay as-is. Let me know how it looks.

    Thread Starter r3ndy

    (@r3ndy)

    Hi @quantum_leap

    Using this CSS makes the layout work properly:

    .comments-title, .comment-list, .comment-navigation {
    order: 1 !important;
    }

    Would it be possible to change the comment navigation into numbered pagination instead of the default previous/next navigation?

    George

    (@quantum_leap)

    Glad to hear it.

    Since GP’s comments.php outputs the prev/next navigation inline (rather than via a hooked function), the cleanest way to swap it for numbered pagination is to copy comments.php into your child theme and edit the two nav blocks.

    Copy wp-content/themes/generatepress/comments.php to wp-content/themes/your-child-theme/comments.php, then in both <nav> blocks (#comment-nav-above and #comment-nav-below):

    1. Add comment-pagination to the class list:
    
    <nav id="comment-nav-above" class="comment-navigation comment-pagination" role="navigation">
    

    2. Replace the two .nav-previous / .nav-next divs with paginate_comments_links():

    
    <?php
    echo paginate_comments_links( array(
        'echo'      => false,
        'prev_text' => __( '&larr; Previous', 'generatepress' ),
        'next_text' => __( 'Next &rarr;', 'generatepress' ),
        'type'      => 'list',
    ) );
    ?>
    

    That outputs a <ul class="page-numbers"> with <li> items for each page, plus next/previous links. Then add this to Customizer → Additional CSS to style it:

    
    .comment-pagination .page-numbers {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        list-style: none;
        margin: 20px 0;
        padding: 0;
    }
    .comment-pagination .page-numbers li {
        margin: 0;
    }
    .comment-pagination .page-numbers a,
    .comment-pagination .page-numbers span {
        display: inline-block;
        padding: 8px 14px;
        border: 1px solid #444;
        border-radius: 4px;
        text-decoration: none;
        line-height: 1;
        color: inherit;
        transition: background 0.15s ease, color 0.15s ease;
    }
    .comment-pagination .page-numbers a:hover {
        background: #444;
        color: #fff;
    }
    .comment-pagination .page-numbers .current {
        background: #7e22ce;
        color: #fff;
        border-color: #7e22ce;
    }
    .comment-pagination .page-numbers .dots {
        border: none;
        padding: 8px 4px;
    }
    

    To summarize, the child theme version of the comments.php file would look like this:

    
    <?php
    /**
     * The template for displaying Comments.
     *
     * @package GeneratePress (modified for numbered comment pagination)
     */
    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    if ( post_password_required() ) {
        return;
    }
    
    /**
     * generate_before_comments hook.
     */
    do_action( 'generate_before_comments' );
    ?>
    <div id="comments">
        <?php
        do_action( 'generate_inside_comments' );
    
        if ( have_comments() ) :
            $comments_number = get_comments_number();
            $comments_title = apply_filters(
                'generate_comment_form_title',
                sprintf(
                    esc_html(
                        /* translators: 1: number of comments, 2: post title */
                        _nx(
                            '%1$s thought on &ldquo;%2$s&rdquo;',
                            '%1$s thoughts on &ldquo;%2$s&rdquo;',
                            $comments_number,
                            'comments title',
                            'generatepress'
                        )
                    ),
                    number_format_i18n( $comments_number ),
                    get_the_title()
                )
            );
    
            // phpcs:ignore -- Title escaped in output.
            echo apply_filters(
                'generate_comments_title_output',
                sprintf(
                    '<h2 class="comments-title">%s</h2>',
                    esc_html( $comments_title )
                ),
                $comments_title,
                $comments_number
            );
    
            do_action( 'generate_below_comments_title' );
    
            if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
                ?>
                <nav id="comment-nav-above" class="comment-navigation comment-pagination" role="navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e( 'Comment navigation', 'generatepress' ); ?></h2>
                    <?php
                    echo paginate_comments_links( array(
                        'echo'      => false,
                        'prev_text' => __( '&larr; Previous', 'generatepress' ),
                        'next_text' => __( 'Next &rarr;', 'generatepress' ),
                        'type'      => 'list',
                    ) );
                    ?>
                </nav><!-- #comment-nav-above -->
            <?php endif; ?>
    
            <ol class="comment-list">
                <?php
                wp_list_comments(
                    array(
                        'callback' => 'generate_comment',
                    )
                );
                ?>
            </ol><!-- .comment-list -->
    
            <?php
            if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
                ?>
                <nav id="comment-nav-below" class="comment-navigation comment-pagination" role="navigation">
                    <h2 class="screen-reader-text"><?php esc_html_e( 'Comment navigation', 'generatepress' ); ?></h2>
                    <?php
                    echo paginate_comments_links( array(
                        'echo'      => false,
                        'prev_text' => __( '&larr; Previous', 'generatepress' ),
                        'next_text' => __( 'Next &rarr;', 'generatepress' ),
                        'type'      => 'list',
                    ) );
                    ?>
                </nav><!-- #comment-nav-below -->
                <?php
            endif;
        endif;
    
        // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
        if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
            ?>
            <p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'generatepress' ); ?></p>
            <?php
        endif;
    
        comment_form();
        ?>
    </div><!-- #comments -->
    



    Thread Starter r3ndy

    (@r3ndy)

    Hi @quantum_leap

    I decided not to switch to numbered comment pagination as it would require major changes. Instead, I followed your initial suggestion, keeping the existing functionality without significant modifications. Thank you for your help and support.

Viewing 4 replies - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.