[英]Inconsistent date format in comments and comment reply link in Wordpress
The code below is the comments template from my functions.php in WP. 下面的代码是我在WP中的functions.php中的注释模板。 It outputs comments with a reply link to every comment. 它输出带有每个评论的回复链接的评论。 When someone replies to a comment it says 当有人回复评论时说
"In reply to Name of comment author + Comment date " “回复评论作者姓名+评论日期 ”
below the comment text. 在评论文本下方。 Clicking that link, which I for simplicity's sake call the "comment-reply-link", gets you back to the original comment. 单击该链接(为简便起见,我将其称为“评论-回复-链接”)使您回到原始评论。 So far all is well. 到目前为止一切都很好。
The date of every comment displays correctly in the format I have chosen ('M j YH:i'), which is in the first printf function. 每个注释的日期均以我选择的格式('M j YH:i')正确显示,该格式在第一个printf函数中。 A comment date will display in the following format "Feb 10 2012 10:35" The date in the "comment-reply-link", however, displays in a completely different date format: 2012-02-10 10:35:17. 注释日期将以以下格式显示“ Feb 10 2012 10:35”,但是“ comment-reply-link”中的日期将以完全不同的日期格式显示:2012-02-10 10:35:17。 As you can see, it also displays seconds. 如您所见,它还会显示秒。
The last printf function in the code below outputs the "comment-reply-link". 下面代码中的最后一个printf函数输出“ comment-reply-link”。 I don't know where the inconsistent format comes from. 我不知道格式不一致的地方。 It's not in my settings in the admin panel nor can I find it in the comments-template.php. 它不在管理面板中的我的设置中,也无法在comment-template.php中找到。 The only place where the incorrect format is also displayed is in the comment_date column in the comments table when I open the database in phpmyadmin. 当我在phpmyadmin中打开数据库时,唯一显示错误格式的地方是在注释表的comment_date列中。
Would be very grateful if someone could help me get the "comment-reply-link" in the same date format as the comments themselves ('M j YH:i'). 如果有人可以帮助我以与评论本身相同的日期格式(“ M j YH:i”)获得“评论-回复-链接”,将不胜感激。
function mytheme_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-body">
<div class="comment-meta commentmetadata">
<div class="commenter-info"><span class="commenter"><?php printf( __( '%s', 'mytheme' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?><br/></span><!-- commenter -->
<div class="comment-date"><?php
/* translators: 1: date, 2: time */
printf( __( '%1$s', 'mytheme' ), get_comment_date('M j Y H:i') ); ?><?php edit_comment_link( __( 'Edit →', 'mytheme' ), ' ' );
?></div><!-- comment-date -->
</div><!-- commenter-info -->
<div class="comment-gravatar"><?php echo get_avatar( $comment, 65 ); ?></div>
<span class="reply">
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
<a class="comment-reply-link" href="?replytocom=<?php comment_ID(); ?>#respond" onclick="return addComment.moveForm('comment-<?php comment_ID(); ?>', '<?php comment_ID(); ?>', 'respond', '<?php echo $post->ID; ?>')">Reply</a>
</span><!-- reply -->
</div><!-- .comment-meta .commentmetadata -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- comment-content -->
<?php
if ( $comment->comment_parent ) {
$parent = get_comment( $comment->comment_parent );
$parent_link = esc_url( get_comment_link( $comment->comment_parent ) );
printf( '<span id="replyto">In reply to <a href="%1$s">%2$s %3$s</a></span>', $parent_link, $parent->comment_author, $parent->comment_date );
} ?>
<?php if ( $comment->comment_approved == '0' ) : ?>
<p class="moderation"><?php _e( 'Your comment is moderated', 'mytheme' ); ?></p>
<?php endif; ?>
</div><!-- comment-body-->
</div><!-- #comment-## -->
It looks like you need to reformat the $parent->comment_date
. 看来您需要重新格式化$parent->comment_date
。
Try adding this: 尝试添加以下内容:
$date = DateTime::createFromFormat( 'Y-m-d H:i:s', $parent->comment_date);
$parent->comment_date = $date->format( 'M j Y H:i');
Before: 之前:
printf( '<span id="replyto">In reply to <a href="%1$s">%2$s %3$s</a></span>', $parent_link, $parent->comment_author, $parent->comment_date );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.