WordPress如何快速设置共用头部模板?
- CMS教程
- 2025-06-16
- 25
在WordPress中设置公共头部模板(header.php)是主题开发的核心环节,直接影响SEO表现和用户体验,以下是符合百度算法和E-A-T原则的详细实现方案:
基础HTML结构(header.php)
<!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <!-- E-A-T优化:权威性声明 --> <?php if (is_single()) : ?> <meta name="author" content="<?php the_author_meta('display_name'); ?>"> <link rel="author" href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"> <?php endif; ?> <!-- 百度SEO必备 --> <meta name="baidu-site-verification" content="您的百度验证码"> <link rel="canonical" href="<?php echo esc_url(home_url($wp->request)); ?>"> <?php wp_head(); ?> </head> <body <?php body_class(); ?>> <!-- 百度自动推送 --> <script> (function(){ var bp = document.createElement('script'); bp.src = '//push.zhanzhang.baidu.com/push.js'; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <header id="site-header" itemscope itemtype="https://schema.org/WPHeader"> <div class="container"> <!-- Logo优化 --> <div class="site-branding" itemscope itemtype="https://schema.org/Organization"> <a href="<?php echo esc_url(home_url('/')); ?>" itemprop="url"> <?php if (has_custom_logo()) : ?> <?php the_custom_logo(); ?> <?php else : ?> <span itemprop="name"><?php bloginfo('name'); ?></span> <?php endif; ?> </a> </div> <!-- 导航菜单 --> <nav class="main-navigation" role="navigation" aria-label="主菜单"> <?php wp_nav_menu([ 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'container' => false, 'depth' => 2 ]); ?> </nav> <!-- 面包屑导航(增强SEO) --> <div class="breadcrumb" itemprop="breadcrumb"> <?php if (function_exists('yoast_breadcrumb')) { yoast_breadcrumb('<p>','</p>'); } ?> </div> </div> </header>
关键优化点解析
-
E-A-T权威性增强
- 文章页添加作者关联链接 (rel="author")
- 组织架构结构化数据 (itemtype="https://schema.org/Organization")
- 使用官方函数获取作者信息 (the_author_meta())
-
百度SEO必备项

- 百度验证码自动嵌入
- 规范链接标签 (rel="canonical")
- 百度自动推送JS代码
- 面包屑导航(需安装Yoast SEO插件)
-
移动友好性
- Viewport声明适配移动端
- 菜单限制二级深度 ('depth' => 2)
- ARIA角色标注 (role="navigation")
-
性能优化

- 避免多余容器 ('container' => false)
- 使用原生WordPress函数输出
- 结构化数据精简加载
-
所有输出使用WordPress安全函数:
- esc_url() 处理链接
- bloginfo('charset') 动态字符集
- body_class() 自动载入安全类名
-
通过W3C验证:

- 使用 language_attributes() 输出语言属性
- Schema.org结构化数据标注
- 正确的HTML5语义化标签
-
禁止行为
- 头部隐藏文字/链接
- 关键词堆砌的meta标签
- 无关外链(尤其导航栏)
-
推荐做法
- 导航使用文字链接(非JS渲染)
- 保持代码量<50KB
- 添加网站所有权验证
引用来源:
- 百度搜索优化指南
- Google E-A-T白皮书
- WordPress官方主题开发标准
- Schema.org结构化数据规范
主题函数补充 (functions.php)
需在主题函数文件中注册菜单和标题支持:
// 注册导航菜单 register_nav_menus([ 'primary' => __('主菜单', 'your-theme-textdomain') ]); 标签 add_theme_support('title-tag');
安全与验证
百度算法注意事项