diff --git a/archive-tucao.php b/archive-tucao.php index 9d83db9..5029153 100644 --- a/archive-tucao.php +++ b/archive-tucao.php @@ -1,6 +1,6 @@ @@ -19,22 +19,38 @@ get_header();
- - 💬 条留言 + + 💬 条留言
get_the_ID(), 'status' => 'approve')); - foreach ($comments as $c) { - echo '
' . esc_html($c->comment_author) . ' · ' . esc_html($c->comment_date) . '
' . esc_html($c->comment_content) . '
'; - } + $comments = get_comments(array('post_id' => get_the_ID(), 'status' => 'approve', 'order' => 'ASC')); + foreach ($comments as $c) : + $reply_to = $c->comment_parent ? get_comment($c->comment_parent) : null; ?> -
- - - -
+
+
+ 'tucao-comment-avatar')); ?> + comment_author); ?> + + 回复 @comment_author); ?> + + +
+
comment_content); ?>
+
+ +
+ +
+ + + + +
+ + + +
@@ -45,4 +61,9 @@ get_header();

暂无微言

+ + + diff --git a/functions.php b/functions.php index 134e91c..104c339 100644 --- a/functions.php +++ b/functions.php @@ -146,6 +146,14 @@ add_filter('get_avatar', function($avatar, $id_or_email, $size, $default, $alt) $uid = 0; if (is_numeric($id_or_email)) $uid = (int) $id_or_email; elseif (is_object($id_or_email) && isset($id_or_email->user_id)) $uid = (int) $id_or_email->user_id; + elseif (is_object($id_or_email) && $id_or_email instanceof WP_Comment) { + $uid = (int) $id_or_email->user_id; + } + // 如果传的是 email 但找不到用户,尝试用 email 找 + if (!$uid && is_string($id_or_email) && strpos($id_or_email, '@') !== false) { + $u = get_user_by('email', $id_or_email); + if ($u) $uid = $u->ID; + } $att_id = get_user_meta($uid, 'custom_avatar', true); if ($att_id) { $url = wp_get_attachment_image_url($att_id, 'medium'); @@ -232,3 +240,29 @@ add_action('init', function() { add_rewrite_rule('^photo/([0-9]+)/([^/]+)/?$', 'index.php?post_type=photo&p=$matches[1]', 'top'); add_rewrite_rule('^photo/([0-9]+)/?$', 'index.php?post_type=photo&p=$matches[1]', 'top'); }); + +// 允许通过 REST API 创建评论 +add_filter('rest_allow_anonymous_comments', '__return_false'); + +// 评论后重定向回当前页 + + +// tucao 不要详情页,直接回 archive +add_action('template_redirect', function() { + if (is_singular('tucao')) { + wp_redirect(get_post_type_archive_link('tucao'), 301); + exit; + } +}); + +// 评论后送回微言列表并锚点定位 +add_filter('comment_post_redirect', function($loc, $comment) { + $post = get_post($comment->comment_post_ID); + if ($post && $post->post_type === 'tucao') { + return get_post_type_archive_link('tucao') . '#tucao-' . $comment->comment_post_ID; + } + return $loc; +}, 99, 2); + +// 中国时区 +date_default_timezone_set('Asia/Shanghai'); diff --git a/style.css b/style.css index 723bbd2..811d17c 100644 --- a/style.css +++ b/style.css @@ -488,3 +488,41 @@ echo "done" .photo-lightbox__content img { border-radius: 6px; } + +.tucao-login-hint { font-size: 12px; color: var(--ink-lighter); margin-top: 12px; } +.tucao-login-hint a { color: var(--cinnabar); } + +.tucao-comment { display:flex; gap:8px; align-items:flex-start; padding:8px 0; } +.tucao-comment-avatar { width:22px !important; height:22px !important; border-radius:50% !important; object-fit:cover !important; flex-shrink:0; } +.tucao-comment-time { font-size:11px; color:var(--ink-lighter); margin-left:4px; } +.tucao-comment-text { font-size:13px; color:var(--ink-light); line-height:1.6; margin-top:2px; } + +.tucao-comment-body { flex:1; } +.tucao-comment-author { font-size:13px; font-weight:600; } +.tucao-comment-avatar { border-radius:50% !important; } + +.tucao-comment { align-items: baseline; } +.tucao-comment-avatar { position: relative; top: 3px; } + +.tucao-comment-body { line-height: 16px; } +.tucao-comment-avatar { top: 0 !important; width:16px !important; height:16px !important; } + +.tucao-comment { padding:6px 0; } +.tucao-comment-meta { display:flex; align-items:center; gap:6px; margin-bottom:4px; } +.tucao-comment-meta > img { width:16px !important; height:16px !important; border-radius:50% !important; object-fit:cover !important; flex-shrink:0; } + +.tucao-comment-meta { display:flex; align-items:baseline; gap:6px; } +.tucao-comment-time { font-size:11px; color:var(--ink-lighter); } + +.tucao-comment-author { font-size:13px; font-weight:600; } +.tucao-comment-time { font-size:11px; color:var(--ink-lighter); } +.tucao-comment-text { font-size:13px; color:var(--ink-light); line-height:1.6; margin-left:22px; } +.tucao-comment { padding:6px 0; border-bottom:1px dashed var(--border-light); display:block !important; } +.tucao-comment:last-child { border-bottom:none; } +.tucao-comment-meta { display:flex !important; align-items:center !important; gap:6px; margin-bottom:2px; } +.tucao-comment-meta > img { width:16px !important; height:16px !important; border-radius:50% !important; object-fit:cover !important; flex-shrink:0; } +.tucao-comment-author { font-size:13px; font-weight:600; } +.tucao-comment-time { font-size:11px; color:var(--ink-lighter); } +.tucao-comment-text { display:block; font-size:13px; color:var(--ink-light); line-height:1.6; margin-left:22px; } + +.tucao-reply-to { font-size:11px; color:var(--cinnabar); margin:0 2px; }