WordPress 中添加水平线分隔符(Horizontal Line)有多种简单方法
在 WordPress 中添加水平线分隔符(Horizontal Line)有多种简单方法,适用于不同场景:
html
预览
然后添加自定义 CSS 样式(可放在主题的
css
添加上述代码后,可以在文章编辑器中使用短代码灵活插入分隔线:
plaintext
方法 1:使用 WordPress 编辑器按钮(Gutenberg 区块编辑器)
- 打开文章或页面编辑器
- 在想要添加分隔线的位置,点击 "+" 号添加新区块
- 搜索并选择 "分隔符"(Separator)区块
- 可以直接使用默认样式,或在右侧面板调整颜色、高度等属性
方法 2:使用快捷键(经典编辑器和 Gutenberg 通用)
在编辑器中输入以下任意一种格式,然后按回车键:---
(三个减号)***
(三个星号)___
(三个下划线)
方法 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%"]
仍然有问题? 我们要如何帮助您?