PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Dec 06, 2019 6:24 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 5:54 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:05 am 
Offline
Moderator
User avatar

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

_________________


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

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

Syntax: [ Download ] [ Hide ]
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


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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:16 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
So you sent me that code example: [url]posting.php?mode=reply&f=2&t=144184#pr709689[/url]
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:

Syntax: [ Download ] [ Hide ]
<?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.


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:20 am 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Looks about right. You'll want to pass the post ID into get_comments, though, otherwise it will return all comments.

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:21 am 
Offline
Moderator
User avatar

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

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:24 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:29 am 
Offline
Moderator
User avatar

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

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:32 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:54 am 
Offline
Moderator
User avatar

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

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 6:57 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 7:09 am 
Offline
Moderator
User avatar

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

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 7:33 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 7:58 am 
Offline
Moderator
User avatar

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

_________________


Top
 Profile  
 
PostPosted: Fri Sep 15, 2017 9:47 am 
Offline
DevNet Master

Joined: Wed Oct 08, 2008 3:39 pm
Posts: 4434
Location: United Kingdom
This is my new comments.php file in the child folder:
Syntax: [ Download ] [ Hide ]
<?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.

Syntax: [ Download ] [ Hide ]

/**
 * 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.


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: No registered users and 1 guest


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