PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 06, 2019 5:42 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 115 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next
Author Message
PostPosted: Fri Sep 15, 2017 11:31 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 11:34 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
"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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 11:47 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.
Syntax: [ Download ] [ Hide ]
$return_value = function_name($first_argument, $second_argument);


Does that clarify my previous comment and point you in the right direction?

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:14 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
Syntax: [ Download ] [ Hide ]
                        <?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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:14 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:17 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:18 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Might also be worth checking out what that callback does.

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:29 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:42 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Is it displaying the correct comments, at least?

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:44 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:45 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
So it was never displaying the empty comments?

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 12:55 pm 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 1:00 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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.

_________________


Top
 Profile  
 
PostPosted: Sat Sep 16, 2017 6:03 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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. 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.

_________________


Top
 Profile  
 
PostPosted: Sat Sep 16, 2017 8:12 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
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
Syntax: [ Download ] [ Hide ]
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'] = <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
Syntax: [ Download ] [ Hide ]
        <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
Syntax: [ Download ] [ Hide ]
        <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.

_________________


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 115 posts ]  Go to page Previous  1, 2, 3, 4, 5, 6, 7, 8  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group