原文网址:WordPress--将文章的H标签降级-CSDN博客
简介
本文介绍WordPress如何将文章的H标签降级。
方法
在functions.php文件里或者Code Snippets插件中添加如下代码:
php
function change_head_level($content) {
$content = preg_replace('/<h5\b([^>]*>)(.*?)<\/h5>/', '<h6$1$2</h6>', $content); // 将 h5 替换为 h6
$content = preg_replace('/<h4\b([^>]*>)(.*?)<\/h4>/', '<h5$1$2</h5>', $content); // 将 h4 替换为 h5
$content = preg_replace('/<h3\b([^>]*>)(.*?)<\/h3>/', '<h4$1$2</h4>', $content); // 将 h3 替换为 h4
$content = preg_replace('/<h2\b([^>]*>)(.*?)<\/h2>/', '<h3$1$2</h3>', $content); // 将 h2 替换为 h3
$content = preg_replace('/<h1\b([^>]*>)(.*?)<\/h1>/', '<h2$1$2</h2>', $content); // 将 h1 替换为 h2
return $content;
}
add_filter('the_content', 'change_head_level');