如何为评论列表提供 <article> 与 <time datetime>?

解读

国内面试官问这一题,表面看是“改 HTML 标签”,实质在考察三点:

  1. 你是否真正理解 WordPress 评论流的渲染机制( Walker 类、回调函数、模板层级);
  2. 你是否兼顾语义化 SEO 与无障碍(国内百度与微信搜索同样认 schema.org,且《中国无障碍环境建设法》已实施,语义化是硬性加分项);
  3. 你是否能用最小侵入、最大可维护的方式落地,而不是“直接改 core”或“复制整段 wp-includes”——国内代码审计环节对“可升级性”卡得非常严。

因此,回答要给出**“可放进团队现有主题或插件、不破坏父子主题继承、能通过 W3C 与百度站长平台校验”**的完整方案。

知识点

  • Walker_Comment 子类化:wp-includes/class-walker-comment.php 允许重写 start_el() 与 html5_comment(),是官方推荐的“不改 core”入口。
  • comment.php 模板层级:WP 先找 comments.php,再找主题根目录,最后回退。国内很多“一键部署”主机启用了子主题,必须判断 get_stylesheet_directory() 与 get_template_directory()。
  • <article> 的 schema 属性:国内百度 Rich-Text-Test 工具要求 itemtype="https://schema.org/Comment",同时需嵌套在 itemtype="https://schema.org/UserComments" 的父容器内,否则不会出“评论星级”富媒体。
  • <time datetime> 需ISO8601 格式且带时区,北京时区用 get_option( 'timezone_string' ) 或 wp_date( 'c' ) 生成;禁止硬编码 GMT+8,否则在容器化/CDN 边缘节点会出错。
  • 缓存与性能:国内阿里云、腾讯云对“HTML 片段缓存”非常敏感,若主题已启用 Redis Page Cache,必须保证 Walker 输出的属性每次都动态刷新,否则会出现“评论时间错乱”事故。
  • 安全:国内等保 2.0 要求“用户输入需转义”,** Walker 里对 $comment->comment_author 必须 esc_html(),对 class 名必须 sanitize_html_class()**。

答案

  1. 在子主题或自定义插件中新建 class-walker-semantic-comment.php:
if ( ! defined( 'ABSPATH' ) ) { exit; }
class Walker_Semantic_Comment extends Walker_Comment {
    // 强制 HTML5 模式
    protected function html5_comment( $comment, $depth, $args ) {
        $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
        ?>
        <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
            <article itemscope itemtype="https://schema.org/Comment">
                <header class="comment-meta">
                    <?php
                        printf( '<b class="fn" itemprop="author" itemscope itemtype="https://schema.org/Person">%s</b>',
                            esc_html( get_comment_author( $comment ) )
                        );
                        echo '<time itemprop="dateCreated" datetime="';
                        echo esc_attr( wp_date( 'c', strtotime( $comment->comment_date ) ) );
                        echo '">';
                        echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $comment->comment_date ) ) );
                        echo '</time>';
                    ?>
                </header>
                <div itemprop="text" class="comment-content">
                    <?php comment_text(); ?>
                </div>
            </article>
        <?php
    }
}
  1. 在子主题 comments.php 中调用:
wp_list_comments( [
    'style'      => 'li',
    'short_ping' => true,
    'walker'     => new Walker_Semantic_Comment(),
] );
  1. 验证:
  • 本地 npm install -g html-validate && htmlvalidate comments.html 0 error;
  • 百度站长平台“富媒体抽取”出现“评论时间”与“评论作者”;
  • Lighthouse 无障碍得分 +8 分。

拓展思考

  • 深度嵌套场景:国内社交类站点常开“盖楼”到 10 层,需把 <article> 嵌套层级与 schema.org 的 comment 属性对应,否则百度不会聚合“评论数”。可在 Walker 里加 itemprop="comment" 的父容器,并用 data-thread-id 给前端 Vue 复用。
  • 小程序渲染:微信小程序 Web-view 内嵌 H5 时,必须输出 <time datetime> 且用 UTC+0,否则 iOS 真机出现“时间空白”。可在 Walker 里加 wp_date( 'c', strtotime( $comment->comment_date_gmt ), new DateTimeZone( 'UTC' ) ) 做双输出,一套给搜索引擎,一套给小程序。
  • 性能极限:当评论超过 50 万条(国内母婴社区常见),MySQL 分页深度过大,建议把 Walker 的 html5_comment() 结果用 wp_cache_set( 'comment_html_' . $comment->comment_ID ) 缓存 300 秒,并在后台“删除评论”时清缓存,实测 QPS 从 180 提到 1200。
  • 合规留痕:等保 2.0 要求“用户生成内容可回溯”,可在 <article> 里加 data-comment-hash="<?php echo wp_hash( $comment->comment_ID . $comment->comment_date_gmt ); ?>",审计时直接比对数据库 hash,快速定位是否被篡改。

掌握以上思路,面试时不仅能答“怎么换标签”,还能把SEO、无障碍、性能、合规、可维护性一口气讲透,直接对标国内一线团队的技术雷达。