PHP Developers Network
http://forums.devnetwork.net/

Wordpress Pagination Links - how do I modify this?
http://forums.devnetwork.net/viewtopic.php?f=2&t=144184
Page 7 of 8

Author:  Celauran [ Mon Sep 18, 2017 11:46 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?


Author:  simonmlewis [ Mon Sep 18, 2017 12:16 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

So this in single-product-reviews.php:
Syntax: [ Download ] [ Hide ]
                        <ol class="commentlist">
                                <?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments', 'end-callback' => 'jbbg_woocommerce_comments_end' ) ) $comments); ?>
                        </ol>

And this in the footer 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;
}


??

Author:  Celauran [ Mon Sep 18, 2017 1:25 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Looks like there's a function missing. Also, hard to comment on what your single page should look like without knowing what it currently looks like. I don't see anything in what you just posted that deals with pagination.

Author:  simonmlewis [ Mon Sep 18, 2017 1:28 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

This is the entire "single-product-reviews.php" file:
Syntax: [ Download ] [ Hide ]
<?php
/**
 * Display single product reviews (comments)
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product-reviews.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see             https://docs.woocommerce.com/document/t ... structure/
 * @author              WooThemes
 * @package     WooCommerce/Templates
 * @version     2.3.2
 */

if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
}

global $product;

if ( ! comments_open() ) {
        return;
}

?>
<div id="reviews" class="woocommerce-Reviews">
        <div id="comments">
                <h2 class="woocommerce-Reviews-title"><?php
                        if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_review_count() ) ) {
                                /* translators: 1: reviews count 2: product name */
                                printf( esc_html( _n( '%1$s review for %2$s', '%1$s reviews for %2$s', $count, 'woocommerce' ) ), esc_html( $count ), '<span>' . get_the_title() . '</span>' );
                        } else {
                                _e( 'Reviews', 'woocommerce' );
                        }
                ?></h2>

                <?php if ( have_comments() ) : ?>
                <div>As we receive an overwhelming number of reviews on our products, we only display the latest 30 reviews.</div><br/>
                        <ol class="commentlist">
                                <?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments', 'end-callback' => 'jbbg_woocommerce_comments_end' ) ) $comments); ?>
                        </ol>

                        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
                                echo '<nav class="woocommerce-pagination">';
                                paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
                                        'prev_text' => '&larr;',
                                        'next_text' => '&rarr;',
                                        'type'      => 'list',
                                ) ) );
                                echo '</nav>';
                        endif; ?>

                <?php else : ?>

                        <p class="woocommerce-noreviews"><?php _e( 'There are no reviews yet.', 'woocommerce' ); ?></p>

                <?php endif; ?>
        </div>

        <?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) : ?>

                <div id="review_form_wrapper">
                        <div id="review_form">
                                <?php
                                        $commenter = wp_get_current_commenter();

                                        $comment_form = array(
                                                'title_reply'          => have_comments() ? __( 'Add a review', 'woocommerce' ) : sprintf( __( 'Be the first to review &ldquo;%s&rdquo;', 'woocommerce' ), get_the_title() ),
                                                'title_reply_to'       => __( 'Leave a Reply to %s', 'woocommerce' ),
                                                'title_reply_before'   => '<span id="reply-title" class="comment-reply-title">',
                                                'title_reply_after'    => '</span>',
                                                'comment_notes_after'  => '',
                                                'fields'               => array(
                                                        'author' => '<p class="comment-form-author">' . '<label for="author">' . esc_html__( 'Name', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
                                                                                '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" aria-required="true" required /></p>',
                                                        'email'  => '<p class="comment-form-email"><label for="email">' . esc_html__( 'Email', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
                                                                                '<input id="email" name="email" type="email" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" aria-required="true" required /></p>',
                                                ),
                                                'label_submit'  => __( 'Submit', 'woocommerce' ),
                                                'logged_in_as'  => '',
                                                'comment_field' => '',
                                        );

                                        if ( $account_page_url = wc_get_page_permalink( 'myaccount' ) ) {
                                                $comment_form['must_log_in'] = '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a review.', 'woocommerce' ), esc_url( $account_page_url ) ) . '</p>';
                                        }

                                        if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
                                                $comment_form['comment_field'] = '<div class="comment-form-rating"><label for="rating">' . esc_html__( 'Your rating', 'woocommerce' ) . '</label><select name="rating" id="rating" aria-required="true" required>
                                                        <option value="">'
. esc_html__( 'Rate&hellip;', 'woocommerce' ) . '</option>
                                                        <option value="5">'
. esc_html__( 'Perfect', 'woocommerce' ) . '</option>
                                                        <option value="4">'
. esc_html__( 'Good', 'woocommerce' ) . '</option>
                                                        <option value="3">'
. esc_html__( 'Average', 'woocommerce' ) . '</option>
                                                        <option value="2">'
. esc_html__( 'Not that bad', 'woocommerce' ) . '</option>
                                                        <option value="1">'
. esc_html__( 'Very poor', 'woocommerce' ) . '</option>
                                                </select></div>'
;
                                        }

                                        $comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . esc_html__( 'Your review', 'woocommerce' ) . ' <span class="required">*</span></label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true" required></textarea></p>';

                                        comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
                                ?>
                        </div>
                </div>

        <?php else : ?>

                <p class="woocommerce-verification-required"><?php _e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>

        <?php endif; ?>

        <div class="clear"></div>
</div>
 


Which part of the function is missing??

Author:  Celauran [ Mon Sep 18, 2017 2:18 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

I don't see the function my_comments_pagination in your functions.php

Author:  simonmlewis [ Mon Sep 18, 2017 2:56 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

sorry I cannot find a function in this thread called that. getting lost....

Author:  Celauran [ Mon Sep 18, 2017 6:31 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?


Author:  Celauran [ Mon Sep 18, 2017 6:35 pm ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?


Author:  simonmlewis [ Tue Sep 19, 2017 3:07 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Oh my goodness, so close. This is what it now does.

It doesn't show the page numbers. So on a product I am on which has three comments, there is no page numbering - perfect.
If set the pagination to 2 per page, it should show a page two, but instead, it 'stops' half way down the page after the description/reviews etc tabs. With debug on, there is no error down there.

If I look in the source code, I can see html, but it's not displaying it. It literally 'stops' after the Reviews tab from rendering anything on screen.

Author:  simonmlewis [ Tue Sep 19, 2017 4:29 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

My bad - smurf is what I added, not smurf.

It works!!!! YAY.

Goodness me. That's amazing. That's a lot of work there isn't it, just to do that.

Author:  Celauran [ Tue Sep 19, 2017 4:58 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

It's actually not that much in the end. The hard part was figuring out where things were failing. I'm glad everything is working now. Do you understand all the changes and how/why they work?

Author:  simonmlewis [ Tue Sep 19, 2017 5:13 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

It starts here:
Syntax: [ Download ] [ Hide ]
                        <ol class="commentlist">
                                <?php
// this puts the current Post_id into the $comments variable.
                                $comments = get_comments(['post_id' => get_the_ID()]);
// The comments variable then uses that in an arrange for 'function($comment)'.
And it seems to tell it to only return a result of $comment->comment_content is NOT empty.
                                $comments = array_filter($comments, function($comment) {
                                    return !empty($comment->comment_content);
                                });
// This then hooks into the wp_list_comments functions in single-products-reviews.php, and add the $comments array filter on the end of it.
                                wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments', 'end-callback' => 'jbbg_woocommerce_comments_end' ) ), $comments); ?>
                        </ol>


Onto the function file (and I know it will prob filter out some stuff here):
Syntax: [ Download ] [ Hide ]
/**
Function to set pagination work only for non-empty comment_contents.
Works with single-product-reviews.php in the child theme folder.
 */

// this creates a function, and uses the $comments variable filter along with the default $args.

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

// don't know about this lot, but I can see it's about arrays of $comments and $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;
    }


So a lot of it is going over my head, such as smurf (shows as smurf on here... the trailing stuff.

Author:  Celauran [ Tue Sep 19, 2017 5:18 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

Well, a lot of it is standard WordPress code, which we don't necessarily need to get into. What I wanted to highlight, really, was the differences between the original functions and their modified versions. There's really one chief difference.

Author:  simonmlewis [ Tue Sep 19, 2017 5:24 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

It has caused a problem I just noticed.
If a comment is awaiting approval, it shows on the Reviews tab (when not logged in, so it' snot down to admin login) as "your comment is awaiting approval.
Something in that function I think is shows comments in the reviews area, even if they are not published yet.

I've switched it back, so will trial it again in the staging area.

Author:  Celauran [ Tue Sep 19, 2017 5:25 am ]
Post subject:  Re: Wordpress Pagination Links - how do I modify this?

You can pass that in as an argument to get_comments

Page 7 of 8 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/