在 WordPress 中添加水平线分隔符(Horizontal Line)有多种简单方法,适用于不同场景:

方法 1:使用 WordPress 编辑器按钮(Gutenberg 区块编辑器)

  1. 打开文章或页面编辑器
  2. 在想要添加分隔线的位置,点击 "+" 号添加新区块
  3. 搜索并选择 "分隔符"(Separator)区块
  4. 可以直接使用默认样式,或在右侧面板调整颜色、高度等属性

方法 2:使用快捷键(经典编辑器和 Gutenberg 通用)

在编辑器中输入以下任意一种格式,然后按回车键:
  • ---(三个减号)
  • ***(三个星号)
  • ___(三个下划线)
WordPress 会自动将其转换为水平线分隔符。

方法 3:使用自定义 HTML 代码

如果需要更个性化的分隔线,可以插入自定义 HTML:
html
预览
<hr class="custom-divider">

然后添加自定义 CSS 样式(可放在主题的 style.css 或自定义 CSS 面板):
css
.custom-divider {
    border: none;
    height: 3px;
    background: #ff6b6b;
    margin: 20px 0;
    width: 50%;
    margin-left: auto;
    margin-right: auto;
}

方法 4:在主题模板中添加固定分隔线

如果需要在主题特定位置(如文章之间、段落之间)固定显示分隔线,可以编辑主题模板文件(如 single.php):
// 添加自定义分隔线短代码
function custom_divider_shortcode($atts) {
    // 默认属性
    $atts = shortcode_atts(
        array(
            'color' => '#e0e0e0',
            'height' => '2px',
            'width' => '100%',
            'margin' => '20px 0',
        ),
        $atts,
        'divider'
    );
    
    return '<hr style="border: none; background: ' . esc_attr($atts['color']) . '; height: ' . esc_attr($atts['height']) . '; width: ' . esc_attr($atts['width']) . '; margin: ' . esc_attr($atts['margin']) . ';">';
}
add_shortcode('divider', 'custom_divider_shortcode');
    

添加上述代码后,可以在文章编辑器中使用短代码灵活插入分隔线:
plaintext
[divider color="#ff9900" height="4px" width="80%"]