如何将WordPress主题分类标题标签<h1>修改为<h2>
解决分类页面多个
标题的修改方案
针对你网站分类/归档页面(如/esports/、/2023/)存在多个
标签的问题,可通过以下两种方式修改,推荐使用子主题钩子替换法,避免主题更新覆盖修改:
方法一:子主题钩子替换(推荐)
- 打开你的WordPress子主题目录下的
functions.php文件 - 添加以下代码:
// 自定义文章标题输出,将归档页的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 );
- 保存文件,刷新分类页面即可生效
方法二:直接修改主题核心文件(不推荐)
如果没有使用子主题,可直接修改Electro主题的核心函数文件:
- 找到主题目录下的
inc/template-functions.php(或inc/wc-template-functions.php),找到electro_post_header函数 - 将函数中
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; }
- 保存文件即可生效,注意:主题更新后该修改会被覆盖
内容的提问来源于stack exchange,提问作者Crypto Coupons
相关产品推荐
相关产品推荐

