要在WordPress文章的主评论中添加楼层号,您可以使用以下代码示例。将以下代码添加到您的主题的functions.php文件中:

function add_comment_floor_number($comment_text, $comment) {
    // 获取当前评论的楼层号
    $comment_floor = get_comment_floor_number($comment);

    // 将楼层号添加到评论文本中
    $comment_text = '<span class="commentfloor">#' . $comment_floor . '</span> ' . $comment_text;

    return $comment_text;
}

function get_comment_floor_number($comment) {
    $comment_id = $comment>comment_ID;

    // 获取当前文章的全部评论
    $comments = get_comments(array(
        'post_id' => $comment>comment_post_ID,
        'status' => 'approve',
    ));

    // 找到当前评论在所有评论中的位置并加1,以得到楼层号
    $comment_floor = array_search($comment, $comments)  1;

    return $comment_floor;
}

add_filter('comment_text', 'add_comment_floor_number', 10, 2);

这段代码会在评论文本前添加一个楼层号,例如 "#1","#2",以此类推。您可以根据需要自定义楼层号的样式和显示方式,只需修改$comment_text的相关部分。

请记住,在修改主题的functions.php文件之前,务必备份您的网站数据,以防出现意外问题。此外,确保您了解如何访问和编辑WordPress文件,以便进行必要的更改。