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

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 »

I can see this on line 34: <?php wp_list_comments( 'type=comment&callback=blade_grve_comments' ); ?>.
So I think that function you sent over yesterday does need tweaks, but not really understanding what tweaks it needs. Must be to do wtih that wp_list_comments, but not quite sure where.
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 »

Get rid of any changes made yesterday. Look at the code example I posted here: posting.php?mode=reply&f=2&t=144184#pr709689

You need to get the comments for the post (see get_comments), filter out the empty comments (see array_filter), then pass the new comments array as the second argument to wp_list_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 »

I am now seeing more and more errors.
It's possibly something else causing it. But when I Google it, I see people having similar problems, but no one is actually quoting the fix.

[text]Notice: Trying to get property of non-object in /var/www/vhosts/staging.site.co.uk/httpdocs/wp/wp-includes/class-wp-query.php on line 3760

Notice: Trying to get property of non-object in /var/www/vhosts/staging.site.co.uk/httpdocs/wp/wp-includes/class-wp-query.php on line 3762

Notice: Trying to get property of non-object in /var/www/vhosts/staging.site.co.uk/httpdocs/wp/wp-includes/class-wp-query.php on line 3764

Notice: Trying to get property of non-object in /var/www/vhosts/staging.site.co.uk/httpdocs/wp/wp-includes/class-wp-query.php on line 3895[/text]

We are about to update our Staging site to use what is live, as that works nice.
Then I can go back and see where we are with this.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
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 »

So you sent me that code example: posting.php?mode=reply&f=2&t=144184#pr709689
Is that purely the code you found in another page, or something you want me to add?

And then you added this to your function.php file:

Code: Select all

<?php

/**
 * Filters down an array of comments to those where comment_content is not an empty string
 *
 * @param array An array of WP_Comment objects (https://developer.wordpress.org/referen ... p_comment/)
 *
 * @return array An array of WP_Comment objects
 */
function filter_empty_comments($comments) {
    // http://php.net/manual/en/function.array-filter.php
    return array_filter($comments, function($comment) {
        return !empty($comment->comment_content);
    });
}

/**
 * Retrieve comments from the database
 * Some arguments may be required here to pull only comments related to the current post
 * See https://developer.wordpress.org/referen ... _comments/
 */
$unfiltered_comments = get_comments();

// Filter out empty comments, see above
$filtered_comments = filter_empty_comments($unfiltered_comments);

// Used in comments template to list comments
// See https://developer.wordpress.org/referen ... _comments/
wp_list_comments([], $filtered_comments);
minus the <?php if needed.

And that's what you did?
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 »

Looks about right. You'll want to pass the post ID into get_comments, though, otherwise it will return all 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 »

If you're just doing this inside your comments template, you can probably do away with the function definition and just do it inline. Function if you're planning on using it elsewhere, but then move the function out of the template.
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 »

Sorry you are losing me again. We already show only !empty comments in the comments/reviews tab. It's the pagination side that we need it to stop from finding empty comments.
I don't know how to pass the post ID into get_comments.
This needs to be done in a function, else WP updates will overwrite it.
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:I don't know how to pass the post ID into get_comments.
I gave you an example.
simonmlewis wrote:This needs to be done in a function, else WP updates will overwrite it.
Not if it's done in your subtheme.
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 »

Ohhhh you mean to put in our child folder a new 'comments.php' file? Is that what you mean?
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 »

Yes, if you have a child theme, that's where you want to put this. If you're using a third party theme and don't have a child theme, consider creating one.
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 »

I do have a theme with a theme-child folder too. so can certainly do that. Trouble is, our comments.php file differs from yours. I sent you the code from it. So not sure what I need to adjust in there?
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 »

Again on the subway into work, so can't really write out any code. You posted a comment template earlier. Where you see wp_list_comments at the bottom is where you want to add in get_comments and array_filter. I can post an example when I get home if needed.
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 »

Cool thanks We are in the process of having our staging site updated so it's not broken.
Where are you based then? In Montreal so your profle says.
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 »

While they're doing that, I again recommend creating a local dev environment to work in. You can deploy from there first to staging, then to production.
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 »

This is my new comments.php file in the child folder:

Code: Select all

<?php

	if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && 'comments.php' == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
		die ( esc_html__( 'This page cannot be opened directly!', 'blade' ) );
	}

	if ( post_password_required() ) {
?>
		<div class="help">
			<p class="no-comments"><?php esc_html_e( 'This post is password protected. Enter the password to view comments.', 'blade' ); ?></p>
		</div>
<?php
		return;
	}
?>

<?php if ( have_comments() ) : ?>

	<!-- Comments -->
	<div id="grve-comments" class="grve-singular-section grve-smallwidth clearfix">
		<div class="grve-container grve-padding-top-md grve-padding-bottom-md grve-border grve-border-top">
			<div class="grve-comments-header">
				<h6 class="grve-comments-number grve-text-dark">
				<?php comments_number( esc_html__( 'no comments', 'blade' ), esc_html__( '1 comment', 'blade' ), '% ' . esc_html__( 'comments', 'blade' ) ); ?>
				</h6>
				<nav class="grve-comment-nav grve-small-text">
					<ul>
				  		<li><?php previous_comments_link(); ?></li>
				  		<li><?php next_comments_link(); ?></li>
				 	</ul>
				</nav>
			</div>
			<ul>
			<?php 
			                $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' ); ?>
			</ul>
		</div>
	</div>
	<!-- End Comments -->

<?php endif; ?>


<?php if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) : ?>

	<div id="grve-comments" class="grve-singular-section clearfix">
		<div class="grve-container grve-padding-top-md grve-padding-bottom-md grve-border grve-border-top">
			<p class="no-comments"><?php esc_html_e( 'Comments are closed.', 'blade' ); ?></p>
		</div>
	</div>

<?php endif; ?>


<?php if ( comments_open() ) : ?>

<?php
		$commenter = wp_get_current_commenter();
		$req = get_option( 'require_name_email' );

		$args = array(
			'id_form'           => 'commentform',
			'id_submit'         => 'grve-comment-submit-button',
			'title_reply'       => esc_html__( 'Leave a Reply', 'blade' ),
			'title_reply_to'    => esc_html__( 'Leave a Reply to', 'blade' ) . ' %s',
			'cancel_reply_link' => esc_html__( 'Cancel Reply', 'blade' ),
			'label_submit'      => esc_html__( 'Submit Comment', 'blade' ),
			'submit_button'     => '<input name="%1$s" type="submit" id="%2$s" class="%3$s" value="%4$s" />',

			'comment_field' =>
				'<div class="grve-form-textarea grve-border">'.
				'<textarea style="resize:none;" id="comment" name="comment" placeholder="' . esc_attr__( 'Your Comment Here...', 'blade' ) . '" cols="45" rows="15" aria-required="true">' .
				'</textarea></div>',

			'must_log_in' =>
				'<p class="must-log-in">' . esc_html__( 'You must be', 'blade' ) .
				'<a href="' .  wp_login_url( get_permalink() ) . '">' . esc_html__( 'logged in', 'blade' ) . '</a> ' . esc_html__( 'to post a comment.', 'blade' ) . '</p>',

			'logged_in_as' =>
				'<div class="logged-in-as grve-small-text">' .  esc_html__('Logged in as','blade') .
				'<a href="' . admin_url( 'profile.php' ) . '"> ' . $user_identity . '</a>. ' .
				'<a href="' . wp_logout_url( get_permalink() ) . '" title="' . esc_attr__( 'Log out of this account', 'blade' ) . '"> ' . esc_html__( 'Log out', 'blade' ) . '</a></div>',

			'comment_notes_before' => '',
			'comment_notes_after' => '' ,

			'fields' => apply_filters(
				'comment_form_default_fields',
				array(
					'author' =>
						'<div class="grve-form-input grve-border">' .
						'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '"' .
						' placeholder="' . esc_attr__( 'Name', 'blade' ) . ' ' . ( $req ? esc_attr__( '(required)', 'blade' ) : '' ) . '" />' .
						'</div>',

					'email' =>
						'<div class="grve-form-input grve-border">' .
						'<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '"' .
						' placeholder="' . esc_attr__( 'E-mail', 'blade' ) . ' ' . ( $req ? esc_attr__( '(required)', 'blade' ) : '' ) . '" />' .
						'</div>',

					'url' =>
						'<div class="grve-form-input grve-border">' .
						'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '"' .
						' placeholder="' . esc_attr__( 'Website', 'blade' )  . '" />' .
						'</div>',
					)
				),
		);
?>
		<div id="grve-comment-form" class="grve-singular-section grve-smallwidth clearfix">
			<div class="grve-container grve-padding-top-md grve-padding-bottom-md">

			<?php
				//Use comment_form() with no parameters if you want the default form instead.
				comment_form( $args );
			?>
			</div>
		</div>


<?php endif;

//Omit closing PHP tag to avoid accidental whitespace output errors.
This is the function in the foot of functions.php.

Code: Select all


/**
 * Filters down an array of comments to those where comment_content is not an empty string
 *
 * @param array An array of WP_Comment objects (https://developer.wordpress.org/referen ... p_comment/)
 *
 * @return array An array of WP_Comment objects
 */
function filter_empty_comments($comments) {
    // http://php.net/manual/en/function.array-filter.php
    return array_filter($comments, function($comment) {
        return !empty($comment->comment_content);
    });
}

/**
 * Retrieve comments from the database
 * Some arguments may be required here to pull only comments related to the current post
 * See https://developer.wordpress.org/referen ... _comments/
 */
$unfiltered_comments = get_comments();

// Filter out empty comments, see above
$filtered_comments = filter_empty_comments($unfiltered_comments);

// Used in comments template to list comments
// See https://developer.wordpress.org/referen ... _comments/
wp_list_comments([], $filtered_comments);
with the function added I get a mass of error that don't actually appear to be mentioning comments.
with the function not there, the page loads ok, with all the pagination of blanks.

Would it help if I sent you a copy of the theme? If that is permitted here.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply