admin管理员组

文章数量:1125596

I'm using the following code to format the author's comment date and remove the time:

function my_change_comment_date_format( $date, $date_format, $comment ) {
    return date( 'd M Y', strtotime( $comment->comment_date ) );
}
add_filter( 'get_comment_date', 'my_change_comment_date_format', 10, 3 );

function wpb_remove_comment_time($date, $d, $comment) { 
    if ( !is_admin() ) {
            return;
    } else { 
            return $date;
    }
}
add_filter( 'get_comment_time', 'wpb_remove_comment_time', 10, 3);

But now, what I got is in the following format:
14 Mar 2013 at

How can I remove the "at" string, at the end of the date?

I'm using the following code to format the author's comment date and remove the time:

function my_change_comment_date_format( $date, $date_format, $comment ) {
    return date( 'd M Y', strtotime( $comment->comment_date ) );
}
add_filter( 'get_comment_date', 'my_change_comment_date_format', 10, 3 );

function wpb_remove_comment_time($date, $d, $comment) { 
    if ( !is_admin() ) {
            return;
    } else { 
            return $date;
    }
}
add_filter( 'get_comment_time', 'wpb_remove_comment_time', 10, 3);

But now, what I got is in the following format:
14 Mar 2013 at

How can I remove the "at" string, at the end of the date?

Share Improve this question asked Mar 15, 2019 at 11:31 n1kkoun1kkou 1251 silver badge6 bronze badges 7
  • And how do you print these comments? – Krzysiek Dróżdż Commented Mar 15, 2019 at 11:37
  • with predefined function wp_list_comments() – n1kkou Commented Mar 15, 2019 at 11:39
  • And do you use any callback function in there? – Krzysiek Dróżdż Commented Mar 15, 2019 at 11:40
  • No, just using it "as it is". Should I use the callback key with a function, to edit the date? Is it possible to reformat the entire output in that callback? – n1kkou Commented Mar 15, 2019 at 11:47
  • 1 You can edit the code to change the whole date and time string similar way the author did. – Max Yudin Commented Mar 15, 2019 at 15:25
 |  Show 2 more comments

3 Answers 3

Reset to default 3

Most likely, the 'at' is coming from the value of $comment->comment_date. If that is the case, and since we have to do with string, you could pass it from str_replace first, in order to remove the ' at', like:

function my_change_comment_date_format( $date, $date_format, $comment ) {
    return date( 'd M Y', strtotime( str_replace(" at", "", $comment->comment_date) ); );
}

Edit: since the above was not the case, I searched a bit and got the following:

  • In your comments.php template you probably have a call to wp_list_comments(), which without parameters, runs with defaults . One of the default arguments is the 'walker' => new Walker_Comment() which construct the html output of the comments. Taking a look at the Walker_Comment class, arround line 300 is the culprit 'at'.

More specifically: printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );, thus getting rid of get_comment_time() will still print at.

To alter the output you can make use of a callback function, passed to the wp_list_comments()

e.g:1) in the comments.php (or in any other template file you want to display the comments)

<?php
// Show comments
wp_list_comments( array(
    'callback' => 'not_default_comments'
) );
?>

2) In your functions.php (or in a seperate file, which you will include in the functions), write the callback code:

<?php
function not_default_comments( $comment, $args, $depth ) {
    global $post;
    $author_id = $post->post_author;
    $GLOBALS['comment'] = $comment;
?>
    <li id="li-comment-<?php comment_ID(); ?>">
        <article id="comment-<?php comment_ID(); ?>" <?php comment_class('clr'); ?>>
            <div class="comment-author vcard">
                <?php echo get_avatar( $comment, 45 ); ?>
            </div><!-- .comment-author -->
            <div class="comment-details clr">
                <header class="comment-meta">
                    <cite class="fn"><?php comment_author_link(); ?></cite>
                    <span class="comment-date">
                    <?php printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
                        esc_url( get_comment_link( $comment->comment_ID ) ),
                        get_comment_time( 'c' ),
                        sprintf( _x( '%1$s', '1: date', 'twenties' ), get_comment_date() )
                    ); ?>
                    </span><!-- .comment-date -->
                </header><!-- .comment-meta -->
                <?php if ( '0' == $comment->comment_approved ) : ?>
                    <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'twenties' ); ?></p>
                <?php endif; ?>
                <div class="comment-content entry clr">
                    <?php comment_text(); ?>
                </div><!-- .comment-content -->
                <div class="reply comment-reply-link">
                    <?php comment_reply_link( array_merge( $args, array(
                        'reply_text' => esc_html__( 'Reply to this message', 'twenties' ),
                        'depth'      => $depth,
                        'max_depth'  => $args['max_depth'] )
                    ) ); ?>
                </div><!-- .reply -->
            </div><!-- .comment-details -->
        </article><!-- #comment-## -->
    <?php
}
  • I got the above example callback function from wpexplorer.com, and sligtly modified it to exclude the '... at time' part.

And that's it. Now your comments will be displayed with your custom template from the callback you have provided.

You can do this much easier with JavaScript if you want:


const commentsTime = document.querySelectorAll("#comments time")
commentsTime?.forEach(el => {
    el.innerText = el.innerText.replace("at", "")
})

Another bit hacky solution is to first echo the date and then return 'false' with the get_comment_date filter:

function custom_comment_date( $date, $d, $comment ) {
    echo date( 'd M Y', strtotime( $comment->comment_date ) );
    return false;
}
add_filter( 'get_comment_date', 'custom_comment_date', 10, 3);

And then hide "at" with CSS:

.comment-meta > a:first-child {
    display: none;
}

本文标签: Remove quotatquot string from wordpress comment date