Wordpress Pagination Links - how do I modify this?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Get rid of the stuff you added to functions.php, pass the filtered comments (output of array_filter) as the second argument to wp_list_comments in your comments template, and that should resolve it.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

"pass the filtered comments (output of array_filter) as the second argument to wp_list_comments in your comments template".
Sorry how? It does sound like it can all be resolve within that comments.php file, but the language you use,"pass the fltered comments as a second argument to wp...", is not in my knowledge.

So I understand and don't just copy/paste, can you show me what you mean, and explain please? Because I want to learn so i can use similar or helps me in future.

Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Let's see what I can manage from my phone.

When you call a function, you have some terminology you'll want to understand. Let me try illustrating with an example.

Code: Select all

$return_value = function_name($first_argument, $second_argument);
Does that clarify my previous comment and point you in the right direction?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

Code: Select all

			<?php 
			                $comments = get_comments(['post_id' => get_the_ID()]);
                $comments = array_filter($comments, function($comment) {
                    return !empty($comment->comment_content);
                });
                $comment = function_name($wp_list_comments, $comments);
			wp_list_comments( 'type=comment&callback=blade_grve_comments' ); ?>
??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

simonmlewis wrote:So I understand and don't just copy/paste, can you show me what you mean, and explain please? Because I want to learn so i can use similar or helps me in future.
That's great. I will again recommend that video series I posted earlier in this thread. Take an afternoon and go through it. Work along where you can. I expect it will help with the fundamentals as well as with providing a vocabulary for things you already know but may not realize you know, or don't know the names of. Of course, feel free to reach out with any questions. These days I definitely have less time than I used to, but I'm always happy to help.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

simonmlewis wrote:

Code: Select all

			<?php 
			                $comments = get_comments(['post_id' => get_the_ID()]);
                $comments = array_filter($comments, function($comment) {
                    return !empty($comment->comment_content);
                });
                $comment = function_name($wp_list_comments, $comments);
			wp_list_comments( 'type=comment&callback=blade_grve_comments' ); ?>
??
No, that's a bit too literal. Try this:

Code: Select all

$comments = get_comments(['post_id' => get_the_ID()]);
                $comments = array_filter($comments, function($comment) {
                    return !empty($comment->comment_content);
                });
                
			wp_list_comments( 'type=comment&callback=blade_grve_comments', $comments);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Might also be worth checking out what that callback does.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

On a product where we have 466 'empties' that are star ratings, and no !empty comment_content fields, it's showing 16 pages within pagination.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Is it displaying the correct comments, at least?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

It always was. It was the pagination at fault.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

So it was never displaying the empty comments?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

When we moved our site from non-wordpress to wordpress, we had to import all comments and star ratings. The Ratings are part of WP tho.
So a team imported all the star ratings comments as blank comments. And the main comments are normal ones.
They managed to make the blank comments not show, but when we got more and more new comments, we turned on pagination, and that still showed ALL comments. ie. it obviously counts all the comments for pagination, and the query for what is "displays" is different.

That is the problem. Pagination only - that's what needs fixing.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Comments and star ratings should be different things IMO. That said, I'm a little surprised reducing the number of comments being returned doesn't seem to affect pagination. Time to dive back into the WP docs.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Spent some more time digging through the underlying WP code this morning and it appears that a number of the functions involved in comment pagination do not accept comments as an argument, so while you've filtered down the list to those that contain comment_content, there's no way to pass that information along. Something like this might be worth exploring, but I don't know if that will work either. You may well need to come up with your own set of functions that leverage a custom query. All of that would probably make most sense in a plugin.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

So, because this was obnoxious, I kept at it a bit. Using the default twentyseventeen theme, I made these quick modifications.

Add to the bottom of functions.php

Code: Select all

function my_comments_pagination($comments = [], $args = []) {
    echo my_get_comments_pagination($comments, $args);
}

function my_get_comments_pagination($comments = [], $args = []) {
    $navigation = '';
    $args       = wp_parse_args( $args, array(
        'screen_reader_text' => __( 'Comments navigation' ),
    ) );
    $args['echo'] = false;

    // Make sure we get plain links, so we get a string we can work with.
    $args['type'] = 'plain';

    $links = my_paginate_comments_links($comments, $args);

    if ( $links ) {
        $navigation = _navigation_markup( $links, 'comments-pagination', $args['screen_reader_text'] );
    }

    return $navigation;
}

function my_paginate_comments_links($comments = [], $args = []) {
    global $wp_rewrite;

    if ( ! is_singular() )
        return;

    $page = get_query_var('cpage');
    if ( !$page )
        $page = 1;
    $max_page = get_comment_pages_count(empty($comments) ? null : $comments);
    $defaults = array(
        'base' => add_query_arg( 'cpage', '%#%' ),
        'format' => '',
        'total' => $max_page,
        'current' => $page,
        'echo' => true,
        'add_fragment' => '#comments'
    );
    if ( $wp_rewrite->using_permalinks() )
        $defaults['base'] = user_<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');

    $args = wp_parse_args( $args, $defaults );
    $page_links = paginate_links( $args );

    if ( $args['echo'] )
        echo $page_links;
    else
        return $page_links;
}
Then I simply changes comments.php from this

Code: Select all

        <ol class="comment-list">
            <?php
                wp_list_comments( array(
                    'avatar_size' => 100,
                    'style'       => 'ol',
                    'short_ping'  => true,
                    'reply_text'  => twentyseventeen_get_svg( array( 'icon' => 'mail-reply' ) ) . __( 'Reply', 'twentyseventeen' ),
                ) );
            ?>
        </ol>

        <?php the_comments_pagination( array(
            'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous', 'twentyseventeen' ) . '</span>',
            'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
        ) );
to this

Code: Select all

        <ol class="comment-list">
            <?php
                $comments = get_comments(['post_id' => get_the_ID()]);
                $comments = array_filter($comments, function($comment) {
                    return !empty($comment->comment_content);
                });
                wp_list_comments( array(
                    'avatar_size' => 100,
                    'style'       => 'ol',
                    'short_ping'  => true,
                    'reply_text'  => twentyseventeen_get_svg( array( 'icon' => 'mail-reply' ) ) . __( 'Reply', 'twentyseventeen' ),
                ), $comments );
            ?>
        </ol>

        <?php my_comments_pagination( $comments, array(
            'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous', 'twentyseventeen' ) . '</span>',
            'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
        ) );
You'll need to tweak it a little to work with your theme, but this should get you most of the way there.
Post Reply