Files
kjweiji/comments.php
T

80 lines
3.5 KiB
PHP

<?php
/**
* 空笺未寄 — 评论模板
*/
if (post_password_required()) return;
?>
<div id="comments" class="comments-section">
<?php if (have_comments()) : ?>
<h3 class="comment-reply-title">笺下留言</h3>
<ol class="comment-list">
<?php
$comments = get_comments(array('post_id' => get_the_ID(), 'status' => 'approve', 'order' => 'ASC'));
foreach ($comments as $c) :
$is_reply = $c->comment_parent > 0;
$parent = $is_reply ? get_comment($c->comment_parent) : null;
?>
<li class="comment-item<?php echo $is_reply ? ' comment-item--reply' : ''; ?>">
<div class="comment-item__meta">
<?php echo get_avatar($c, 18, '', '', array('class' => 'comment-avatar')); ?>
<span class="comment-item__author"><?php echo esc_html($c->comment_author); ?></span>
<?php if ($parent) : ?>
<span class="comment-item__replyto">回复 @<?php echo esc_html($parent->comment_author); ?></span>
<?php endif; ?>
<span class="comment-item__time"><?php echo get_comment_date('Y-m-d H:i:s', $c); ?></span>
</div>
<div class="comment-item__text"><?php echo esc_html($c->comment_content); ?></div>
<div class="comment-item__reply">
<?php if (is_user_logged_in()) : ?>
<a href="<?php echo esc_url(add_query_arg('replytocom', $c->comment_ID, get_permalink() . '#respond')); ?>">回复</a>
<?php else : ?>
<a href="javascript:void(0)" onclick="goLogin()">回复</a>
<?php endif; ?>
</div>
</li>
<?php endforeach; ?>
</ol>
<?php endif; ?>
<?php if (is_user_logged_in()) : ?>
<?php
comment_form(array(
'title_reply' => '',
'comment_notes_before' => '',
'comment_notes_after' => '',
'logged_in_as' => '',
'fields' => array(),
'comment_field' => '<textarea id="comment" name="comment" rows="4" placeholder="写点什么..." required></textarea>',
'submit_button' => '<button type="submit" class="submit">落笔留痕</button>',
'submit_field' => '<div class="form-submit">%1$s %2$s</div>',
'label_submit' => '落笔留痕',
));
?>
<?php else : ?>
<form id="commentform-guest" class="comment-form" novalidate>
<textarea id="comment" name="comment" rows="4" placeholder="写点什么..." required></textarea>
<div class="form-submit">
<button type="submit" class="submit">落笔留痕</button>
</div>
</form>
<script>
document.getElementById('commentform-guest').addEventListener('submit', function(e) {
e.preventDefault();
var m = document.getElementById('login-modal');
if (m) m.classList.add('active');
});
function closeLoginModal() {
var m = document.getElementById('login-modal');
if (m) m.classList.remove('active');
}
function goLogin() {
window.location.href = '/wp-login.php?redirect_to=' + encodeURIComponent(window.location.href);
}
</script>
<?php endif; ?>
</div>