v1.5 — 微言评论 + Photo 画廊

微言评论:
- 需登录后留言,未登录提示
- PHP 后端渲染评论,本地证件照头像
- 平级评论 + @回复引用
- 时间线 ASC 排列
- 评论无需审核直接显示
- tucao 禁用单独详情页
- 评论后锚点定位回列表

Photo 画廊:
- 灯箱全屏查看,多图左右切换
- 单图/边界隐藏方向键
- 图片去重
- 顶部显示标题·作者
This commit is contained in:
2026-05-04 02:35:58 +08:00
parent 55cfd26162
commit 58ef5f2635
3 changed files with 107 additions and 14 deletions
+34
View File
@@ -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');