WordPress--将文章的H标签降级

原文网址: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');
相关推荐
豆豆6 天前
2026 主流 CMS 建站系统对比分析:企业、个人建站该如何选型?
wordpress·建站系统·cms建站·企业建站·建站选型·saas建站·2026建站指南
VIP_CQCRE8 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 AI 写作变成可积累的 SEO 内容资产
ai·api·seo·wordpress·acedatacloud
VIP_CQCRE15 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 SEO 内容站变成长期增长资产
ai·自动化·seo·wordpress·acedatacloud
2601_9657984719 天前
Renovate Theme Setup: Fast Contractor & Construction Site Architecture
theme·wordpress
2601_9657984720 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
2601_9657984723 天前
Boutique Business Consulting WordPress Architecture & SEO Setup
android·theme·wordpress
2601_9657984724 天前
Plastic Surgery Clinic Web Setup: Deploying Rejuvita WordPress
php·theme·wordpress
2601_9657984724 天前
Contractor Web Setup: Deploying Bathrooms & Kitchens Theme Fast
前端·web3·php·wordpress
e6zzseo1 个月前
wordpress独立站seo怎么做才有效
seo·wordpress·独立站·内容优化·外链建设
Web极客码1 个月前
如何让用户订阅WordPress评论?提升用户互动的有效策略
运维·服务器·wordpress