You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将WordPress主题分类标题标签<h1>修改为<h2>

解决分类页面多个
标题的修改方案

针对你网站分类/归档页面(如/esports/、/2023/)存在多个

标签的问题,可通过以下两种方式修改,推荐使用子主题钩子替换法,避免主题更新覆盖修改:

方法一:子主题钩子替换(推荐)

  1. 打开你的WordPress子主题目录下的functions.php文件
  2. 添加以下代码:
// 自定义文章标题输出,将归档页的h1改为h2
function custom_electro_post_header() {
    if ( is_singular() ) :
        // 单篇文章/页面保持h1
        the_title( '<h1 class="entry-title">', '</h1>' );
    else :
        // 分类/归档页将标题改为h2
        the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
    endif;
}

// 移除主题默认的标题输出钩子
remove_action( 'electro_loop_post', 'electro_post_header', 10 );
// 添加自定义的标题输出钩子,保持原优先级
add_action( 'electro_loop_post', 'custom_electro_post_header', 10 );
  1. 保存文件,刷新分类页面即可生效

方法二:直接修改主题核心文件(不推荐)

如果没有使用子主题,可直接修改Electro主题的核心函数文件:

  1. 找到主题目录下的inc/template-functions.php(或inc/wc-template-functions.php),找到electro_post_header函数
  2. 将函数中else分支的<h1>标签替换为<h2>:
function electro_post_header() {
    if ( is_singular() ) :
        the_title( '<h1 class="entry-title">', '</h1>' );
    else :
        // 将此处的h1改为h2
        the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
    endif;
}
  1. 保存文件即可生效,注意:主题更新后该修改会被覆盖

内容的提问来源于stack exchange,提问作者Crypto Coupons

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 03:07:52