<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<?php
// 修复title:统一使用wp_get_document_title,做空值兜底,废弃wp_title
$doc_title = wp_get_document_title();
if(empty(trim($doc_title))){
$doc_title = get_bloginfo('name') . ' | ' . get_bloginfo('description');
}
?>
<title><?php echo esc_html($doc_title); ?></title>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ================== AI 收录优化开始 ================== -->
<!-- 通用robots,所有爬虫(搜索引擎+AI爬虫)优先读取 -->
<meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1">
<!-- 各主流AI爬虫独立授权(锦上添花) -->
<meta name="GPTBot" content="index, follow">
<meta name="Google-Extended" content="index, follow">
<meta name="Claude-Web" content="index, follow">
<meta name="PerplexityBot" content="index, follow">
<meta name="Bytespider" content="index, follow">
<meta name="OAI-SearchBot" content="index, follow">
<!-- canonical 完整覆盖所有页面,解决重复内容对AI收录伤害 -->
<?php
if ( is_singular() ) {
$canonical_url = get_permalink();
} elseif ( is_front_page() || is_home() ) {
$canonical_url = home_url('/');
} elseif ( is_archive() ) {
$canonical_url = get_pagenum_link( max(1, get_query_var('paged') ) );
} elseif ( is_search() ) {
$canonical_url = home_url('/?s=' . urlencode(get_search_query()));
} else {
$canonical_url = home_url('/');
}
echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />'."\n";
?>
<!-- llms.txt LLM爬虫新兴标准 -->
<link rel="alternate" type="text/plain" href="<?php echo esc_url(home_url('/llms.txt')); ?>">
<!-- WebSite Schema结构化数据 -->
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "WebSite",
"name": "<?php echo esc_js(get_bloginfo('name')); ?>",
"description": "<?php echo esc_js(get_bloginfo('description')); ?>",
"url": "<?php echo esc_url(home_url()); ?>",
"inLanguage": "<?php echo esc_attr(get_locale()); ?>",
"potentialAction": {
"@type": "SearchAction",
"target": "<?php echo esc_url(home_url()); ?>/?s={search_term_string}",
"query-input": "required name=search_term_string"
},
"publisher": {
"@type": "Organization",
"name": "<?php echo esc_js(get_bloginfo('name')); ?>",
"logo": {
"@type": "ImageObject",
"url": "<?php
if (has_custom_logo()) {
$custom_logo_id = get_theme_mod('custom_logo');
$image = wp_get_attachment_image_src($custom_logo_id, 'full');
echo esc_url($image[0]);
} else {
echo esc_url(get_site_icon_url());
}
?>"
}
}
}
</script>
<!-- ================== AI 收录优化结束 ================== -->
<?php wp_head(); ?>
<style>
/* ===== 导航条整体样式 ===== */
.main-navigation {
background-color: #ffffff;
border-bottom: 1px solid rgba(0,0,0,0.06);
box-shadow: 0 2px 12px rgba(0,0,0,0.02);
padding: 6px 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
position: relative;
z-index: 99;
}
.nav-container {
max-width: 1440px;
margin: 0 auto;
padding: 0 24px;
display: flex;
flex-wrap: wrap;
align-items: center;
column-gap: 16px;
row-gap: 8px;
}
/* ===== Logo ===== */
.site-logo {
flex: 0 0 auto;
}
.site-logo a {
text-decoration: none;
display: flex;
align-items: center;
column-gap: 6px;
font-size: 1.6rem;
font-weight: 600;
color: #1e1e1e;
letter-spacing: -0.3px;
}
.site-logo img {
max-height: 44px;
width: auto;
display: block;
}
.site-name {
background: linear-gradient(135deg, #1e1e1e 0%, #3a3a3a 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
/* ===== 右侧顶部容器 ===== */
.nav-top-right {
display: flex;
align-items: center;
column-gap: 8px;
flex: 0 0 auto;
margin-left: auto;
}
/* ===== 搜索框 ===== */
.search-box-modern {
flex: 0 0 auto;
}
.search-box-modern .search-form {
display: flex;
width: 100%;
max-width: 280px;
height: 44px;
background: #f5f7fa;
border-radius: 60px;
overflow: hidden;
border: 1px solid #e9edf2;
transition: all 0.25s ease;
}
.search-box-modern .search-form:focus-within {
border-color: #0693e3;
box-shadow: 0 0 0 4px rgba(6, 147, 227, 0.10);
background: #ffffff;
}
.search-box-modern .search-input {
flex: 1;
border: 0;
outline: none;
padding: 0 18px;
font-size: 0.95rem;
background: transparent;
color: #1e1e1e;
min-width: 80px;
}
.search-box-modern .search-input::placeholder {
color: #a0a8b4;
font-weight: 400;
}
.search-box-modern .search-submit {
width: 48px;
height: 100%;
border: 0;
background: transparent;
color: #3d4a5c;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: color 0.2s, background 0.2s;
border-radius: 0 60px 60px 0;
}
.search-box-modern .search-submit:hover {
color: #0693e3;
background: rgba(6, 147, 227, 0.06);
}
.search-box-modern .search-submit svg {
stroke: currentColor;
}
/* ===== 用户区域 ===== */
.nav-user-area {
display: flex;
align-items: center;
column-gap: 12px;
flex: 0 0 auto;
}
.publish-btn {
text-decoration: none;
padding: 6px 16px;
border-radius: 30px;
font-size: 0.95rem;
font-weight: 500;
background-color: #007cba;
color: #fff;
transition: all 0.2s;
white-space: nowrap;
}
.publish-btn:hover {
background-color: #0693e3;
color: #fff;
}
.user-links {
display: flex;
align-items: center;
column-gap: 6px;
font-size: 0.95rem;
}
.user-links a {
color: #2c2c2c;
text-decoration: none;
padding: 6px 14px;
border-radius: 30px;
font-weight: 500;
transition: all 0.2s;
}
.user-links a:hover {
background: #f0f2f5;
color: #007cba;
}
.user-links .sep {
color: #d0d7e2;
font-weight: 300;
}
.user-links a.register-link {
color: #2c2c2c;
background: transparent;
padding: 6px 14px;
}
.user-links a.register-link:hover {
background: #f0f2f5;
color: #007cba;
}
.user-profile {
position: relative;
display: flex;
align-items: center;
column-gap: 8px;
cursor: pointer;
padding: 4px 12px 4px 6px;
border-radius: 40px;
transition: background 0.2s;
background: transparent;
}
.user-profile:hover {
background: #f0f2f5;
}
.user-avatar img {
border-radius: 50%;
width: 34px;
height: 34px;
display: block;
border: 2px solid #fff;
box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}
.user-name {
font-size: 0.9rem;
font-weight: 500;
color: #1e1e1e;
max-width: 90px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.user-dropdown {
display: none;
position: absolute;
top: calc(100% + 6px);
right: 0;
background: #ffffff;
box-shadow: 0 12px 32px rgba(0,0,0,0.10);
border-radius: 16px;
min-width: 150px;
padding: 8px 0;
list-style: none;
margin: 0;
border: 1px solid #f0f0f0;
backdrop-filter: blur(4px);
z-index: 100;
}
.user-dropdown.open {
display: block;
}
.user-dropdown li {
margin: 0;
}
.user-dropdown a {
display: block;
padding: 10px 20px;
color: #2c2c2c;
text-decoration: none;
font-size: 0.9rem;
transition: background 0.15s;
}
.user-dropdown a:hover {
background: #f5f8fc;
color: #007cba;
}
/* ===== 菜单(单独占一行) ===== */
.nav-menu-wrapper {
flex: 0 0 100%;
order: 10;
width: 100%;
border-top: 1px solid #f0f0f0;
padding-top: 8px;
margin-top: 4px;
}
#primary-menu {
display: flex;
list-style: none;
margin: 0;
padding: 0;
column-gap: 4px;
flex-wrap: wrap;
}
#primary-menu > li {
margin: 0;
position: relative;
}
#primary-menu > li > a {
display: block;
padding: 10px 16px;
color: #2c2c2c;
text-decoration: none;
font-size: 0.95rem;
font-weight: 500;
border-radius: 30px;
transition: all 0.2s ease;
letter-spacing: 0.2px;
}
#primary-menu > li > a:hover {
background-color: #f0f2f5;
color: #007cba;
}
#primary-menu .current-menu-item > a,
#primary-menu .current_page_item > a {
background-color: #eef5fb;
color: #007cba;
}
/* 下拉菜单 桌面hover显示 */
#primary-menu ul {
display: none;
position: absolute;
top: 110%;
left: 0;
background: #ffffff;
box-shadow: 0 12px 32px rgba(0,0,0,0.08);
border-radius: 16px;
padding: 8px 0;
min-width: 200px;
list-style: none;
border: 1px solid #f0f0f0;
backdrop-filter: blur(4px);
z-index: 100;
}
#primary-menu li:hover > ul {
display: block;
}
#primary-menu ul li {
margin: 0;
}
#primary-menu ul a {
display: block;
padding: 8px 20px;
color: #2c2c2c;
text-decoration: none;
font-size: 0.9rem;
transition: background 0.15s;
}
#primary-menu ul a:hover {
background-color: #f5f8fc;
color: #007cba;
}
/* 二级菜单下拉箭头 */
.menu-item-has-children > a::after {
content: "▾";
margin-left: 6px;
font-size: 0.7rem;
opacity: 0.6;
}
/* ===== 汉堡按钮 ===== */
.menu-toggle {
display: none;
background: transparent;
border: none;
padding: 8px 12px;
border-radius: 30px;
cursor: pointer;
font-size: 0.95rem;
font-weight: 500;
color: #1e1e1e;
align-items: center;
column-gap: 8px;
transition: background 0.2s;
flex: 0 0 auto;
}
.menu-toggle:hover {
background: #f0f2f5;
}
.menu-toggle .hamburger {
display: flex;
flex-direction: column;
justify-content: space-between;
width: 24px;
height: 18px;
flex-shrink: 0;
}
.menu-toggle .hamburger span {
display: block;
height: 2.5px;
width: 100%;
background-color: #1e1e1e;
border-radius: 4px;
transition: all 0.25s ease;
}
.menu-toggle[aria-expanded="true"] .hamburger span:nth-child(1) {
transform: translateY(7.5px) rotate(45deg);
}
.menu-toggle[aria-expanded="true"] .hamburger span:nth-child(2) {
opacity: 0;
transform: scaleX(0);
}
.menu-toggle[aria-expanded="true"] .hamburger span:nth-child(3) {
transform: translateY(-7.5px) rotate(-45deg);
}
.menu-toggle .menu-label {
font-size: 0.9rem;
font-weight: 500;
color: #2c2c2c;
}
/* ===== 响应式 ===== */
@media screen and (max-width: 820px) {
.nav-container {
padding: 0 16px;
column-gap: 12px;
row-gap: 6px;
}
.search-box-modern .search-form {
max-width: 200px;
height: 40px;
}
.search-box-modern .search-input {
font-size: 0.9rem;
padding: 0 14px;
}
.search-box-modern .search-submit {
width: 42px;
}
.publish-btn {
padding: 5px 14px;
font-size: 0.9rem;
}
}
@media screen and (max-width: 720px) {
.nav-container {
padding: 0 12px;
column-gap: 8px;
row-gap: 4px;
}
.site-logo a {
font-size: 1.3rem;
}
.site-logo img {
max-height: 36px;
}
.search-box-modern .search-form {
max-width: 150px;
height: 36px;
}
.search-box-modern .search-input {
font-size: 0.8rem;
padding: 0 10px;
min-width: 50px;
}
.search-box-modern .search-submit {
width: 36px;
}
.publish-btn {
padding: 4px 12px;
font-size: 0.8rem;
}
.user-avatar img {
width: 30px;
height: 30px;
}
.user-name {
display: none;
}
.user-profile {
padding: 2px 6px 2px 4px;
}
.user-links .register-link {
padding: 4px 12px;
font-size: 0.8rem;
}
.user-links .login-link {
padding: 4px 8px;
font-size: 0.8rem;
}
.user-links .sep {
font-size: 0.8rem;
}
.menu-toggle {
display: flex;
padding: 4px 8px;
column-gap: 4px;
}
.menu-toggle .hamburger {
width: 20px;
height: 16px;
}
.menu-toggle .hamburger span {
height: 2px;
}
.menu-toggle[aria-expanded="true"] .hamburger span:nth-child(1) {
transform: translateY(6.5px) rotate(45deg);
}
.menu-toggle[aria-expanded="true"] .hamburger span:nth-child(3) {
transform: translateY(-6.5px) rotate(-45deg);
}
.menu-toggle .menu-label {
font-size: 0.8rem;
}
.nav-menu-wrapper {
flex: 0 0 100%;
order: 10;
border-top: 1px solid #f0f0f0;
padding-top: 8px;
margin-top: 4px;
}
#primary-menu {
display: none;
flex-direction: column;
width: 100%;
background: #ffffff;
border-radius: 16px;
padding: 4px 0;
box-shadow: 0 4px 16px rgba(0,0,0,0.04);
border: 1px solid #f0f0f0;
column-gap: 0;
}
#primary-menu.open {
display: flex;
}
#primary-menu > li > a {
padding: 12px 20px;
border-radius: 0;
font-size: 1rem;
}
#primary-menu ul {
position: static;
box-shadow: none;
border: none;
padding: 0 0 0 20px;
border-radius: 0;
background: #fafcfd;
margin: 4px 0;
display: none !important;
}
#primary-menu ul.open {
display: block !important;
}
#primary-menu ul a {
padding: 10px 20px;
font-size: 0.9rem;
}
#primary-menu li:hover > ul {
display: none !important;
}
}
@media screen and (max-width: 480px) {
.nav-container {
padding: 0 8px;
column-gap: 6px;
row-gap: 3px;
}
.search-box-modern .search-form {
max-width: 100px;
height: 30px;
}
.search-box-modern .search-input {
font-size: 0.7rem;
padding: 0 6px;
min-width: 40px;
}
.search-box-modern .search-submit {
width: 28px;
}
.search-box-modern .search-submit svg {
width: 12px;
height: 12px;
}
.publish-btn {
padding: 2px 8px;
font-size: 0.7rem;
}
.menu-toggle .menu-label {
display: none;
}
.menu-toggle {
padding: 4px 6px;
}
.site-logo a {
font-size: 1.1rem;
}
.site-logo img {
max-height: 30px;
}
.user-avatar img {
width: 24px;
height: 24px;
}
.user-profile {
padding: 2px 4px;
}
.user-links .register-link {
padding: 2px 8px;
font-size: 0.7rem;
}
.user-links .login-link {
padding: 2px 6px;
font-size: 0.7rem;
}
.user-links .sep {
font-size: 0.7rem;
}
}
</style>
</head>
<body <?php body_class(); ?>>
<nav id="site-navigation" class="main-navigation">
<div class="nav-container">
<!-- Logo -->
<div class="site-logo">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
<?php
if ( has_custom_logo() ) {
the_custom_logo();
} else {
echo '<strong class="site-name">' . esc_html( get_bloginfo( 'name' ) ) . '</strong>';
}
?>
</a>
</div>
<!-- 右侧组合:搜索 + 用户 + 汉堡 -->
<div class="nav-top-right">
<!-- 搜索框 -->
<div class="search-box-modern">
<form role="search" method="get" class="search-form" action="<?php echo esc_url( home_url('/') ); ?>">
<input type="search" class="search-input" placeholder="搜索..." value="<?php echo esc_attr( get_search_query() ); ?>" name="s">
<button type="submit" class="search-submit" aria-label="搜索">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
</button>
</form>
</div>
<!-- 用户区域 -->
<div class="nav-user-area">
<?php
$publish_url = 'https://yixing51.com/selection-popup';
$publish_rel = '';
if ( ! is_user_logged_in() ) {
$publish_url = wp_login_url( $publish_url );
$publish_rel = 'rel="nofollow"';
}
?>
<a href="<?php echo esc_url( $publish_url ); ?>" class="publish-btn" <?php echo $publish_rel; ?>>发布</a>
<?php if ( is_user_logged_in() ) :
$current_user = wp_get_current_user();
?>
<div class="user-profile">
<div class="user-avatar">
<?php echo get_avatar( $current_user->ID, 34, '', esc_attr( $current_user->display_name ) ); ?>
</div>
<span class="user-name"><?php echo esc_html( $current_user->display_name ); ?></span>
<ul class="user-dropdown">
<li><a href="<?php echo esc_url( get_edit_user_link() ); ?>">个人中心</a></li>
<li><a href="<?php echo esc_url( wp_logout_url( home_url() ) ); ?>">退出登录</a></li>
</ul>
</div>
<?php else : ?>
<div class="user-links">
<a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>" class="login-link">登录</a>
<span class="sep">|</span>
<a href="<?php echo esc_url( wp_registration_url() ); ?>" class="register-link">注册</a>
</div>
<?php endif; ?>
</div>
<!-- 汉堡按钮 -->
<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
<span class="hamburger">
<span></span>
<span></span>
<span></span>
</span>
<span class="menu-label">菜单</span>
</button>
</div>
<!-- 菜单 -->
<div class="nav-menu-wrapper">
<?php
wp_nav_menu( array(
'theme_location' => 'header_main',
'menu_id' => 'primary-menu',
'container' => false,
'fallback_cb' => false,
'depth' => 2,
) );
?>
</div>
</div>
</nav>
<script>
document.addEventListener('DOMContentLoaded', function() {
var toggle = document.querySelector('.menu-toggle');
var menu = document.querySelector('#primary-menu');
var userProfile = document.querySelector('.user-profile');
var userDropdown = document.querySelector('.user-dropdown');
// 汉堡切换主菜单
if (toggle && menu) {
toggle.addEventListener('click', function(e) {
e.preventDefault();
var expanded = this.getAttribute('aria-expanded') === 'true' ? false : true;
this.setAttribute('aria-expanded', expanded);
menu.classList.toggle('open');
if(userDropdown) userDropdown.classList.remove('open');
});
}
// 头像下拉菜单
if(userProfile && userDropdown){
userProfile.addEventListener('click', function(e){
e.stopPropagation();
userDropdown.classList.toggle('open');
if(menu) menu.classList.remove('open');
if(toggle) toggle.setAttribute('aria-expanded','false');
});
}
// 移动端二级菜单点击展开
var itemsWithChildren = document.querySelectorAll('#primary-menu .menu-item-has-children > a');
itemsWithChildren.forEach(function(item) {
item.addEventListener('click', function(e) {
if (window.innerWidth <= 720) {
e.preventDefault();
var sub = this.nextElementSibling;
if (sub && sub.tagName === 'UL') {
sub.classList.toggle('open');
}
}
});
});
// 点击空白处关闭
document.addEventListener('click', function(e) {
if(userDropdown && !userProfile.contains(e.target)){
userDropdown.classList.remove('open');
}
if (window.innerWidth > 720) return;
if (!menu || !toggle) return;
var wrap = document.querySelector('.nav-menu-wrapper');
if (!wrap.contains(e.target) && !toggle.contains(e.target)) {
menu.classList.remove('open');
toggle.setAttribute('aria-expanded', 'false');
document.querySelectorAll('#primary-menu ul.open').forEach(function(sub) {
sub.classList.remove('open');
});
}
});
// 窗口放大重置
window.addEventListener('resize', function() {
if (window.innerWidth > 720 && menu) {
menu.classList.remove('open');
if(toggle) toggle.setAttribute('aria-expanded','false');
document.querySelectorAll('#primary-menu ul.open').forEach(function(el) {
el.classList.remove('open');
});
}
if(userDropdown) userDropdown.classList.remove('open');
});
// 键盘无障碍
if(toggle){
toggle.addEventListener('keydown', function(e){
if(e.key === 'Enter' || e.key === ' '){
e.preventDefault();
this.click();
}
})
}
});
</script>

✅ 优点(做得好的地方)
1. AI收录优化非常专业
- 覆盖了主流AI爬虫(GPTBot、Claude、Google-Extended等)
- 有完整的
canonical逻辑 - 嵌入
WebSiteSchema 结构化数据 - 引用
llms.txt新兴标准 - 这部分是亮点,一般WordPress主题不会做得这么全
2. 响应式设计成熟
- 断点合理(820px、720px、480px)
- 小屏下搜索框、按钮、头像尺寸逐级收缩
- 汉堡菜单+二级菜单折叠,交互逻辑完整
3. 用户体验细节
- 搜索框聚焦有高亮动效
- 菜单hover平滑过渡
- 用户头像下拉菜单
- 非登录用户”发布”按钮自动跳转登录,并带
nofollow
4. JavaScript交互完善
- 汉堡菜单/用户下拉/二级菜单三者的互斥关闭逻辑
- 点击外部关闭、resize重置、键盘无障碍支持
- 代码整洁,无多余依赖
要让代码完美工作,你需要:
- □ 在
functions.php中注册header_main菜单位置 - □ 在后台”外观 > 菜单”创建一个菜单并分配到此位置
- □ 生成或删除
/llms.txt引用 - □ 确保有自定义Logo或站点图标,避免Schema空值
- □ 如有需要,调整
publish_url为你的实际发布页面地址 - □ 测试用户未登录/登录两种状态下的表现
1️⃣ 在 functions.php 中注册菜单(必须)
在你的主题 functions.php 文件末尾添加:
/**
* 注册导航菜单
*/
function mytheme_register_nav_menus() {
register_nav_menus( array(
'header_main' => __( '主菜单(顶部导航)', 'your-text-domain' ),
) );
}
add_action( 'after_setup_theme', 'mytheme_register_nav_menus' );
/**
* 如果用户没有创建菜单,显示一个友好的提示
*/
function mytheme_primary_menu_fallback() {
echo '<ul id="primary-menu" class="menu">';
echo '<li class="menu-item"><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '">' . __( '请在这里创建主菜单 →', 'your-text-domain' ) . '</a></li>';
echo wp_list_pages( array(
'title_li' => '',
'depth' => 1,
'echo' => 0,
) );
echo '</ul>';
}
然后在你的导航代码中,把 wp_nav_menu 的调用改为:
<?php
wp_nav_menu( array(
'theme_location' => 'header_main',
'menu_id' => 'primary-menu',
'container' => false,
'depth' => 2,
'fallback_cb' => 'mytheme_primary_menu_fallback', // 改为我们上面定义的函数
) );
?>
✅ 这样即使后台没创建菜单,也会显示页面列表 + 一个引导提示,不会空白。

2️⃣ 自动生成 llms.txt 文件(推荐)
在你的主题 functions.php 中添加以下代码,当访问 /llms.txt 时动态生成内容,无需手动创建文件:
/**
* 动态生成 llms.txt(AI爬虫标准协议文件)
*/
add_action( 'init', 'mytheme_llms_txt_rewrite' );
function mytheme_llms_txt_rewrite() {
add_rewrite_rule( '^llms\.txt$', 'index.php?llms_txt=1', 'top' );
}
add_filter( 'query_vars', 'mytheme_llms_txt_query_var' );
function mytheme_llms_txt_query_var( $vars ) {
$vars[] = 'llms_txt';
return $vars;
}
add_action( 'template_redirect', 'mytheme_serve_llms_txt' );
function mytheme_serve_llms_txt() {
if ( get_query_var( 'llms_txt' ) !== '1' ) {
return;
}
header( 'Content-Type: text/plain; charset=utf-8' );
header( 'X-Robots-Tag: noindex, follow' );
$site_url = home_url( '/' );
$site_name = get_bloginfo( 'name' );
$site_desc = get_bloginfo( 'description' );
// 获取最近的文章列表(供AI参考)
$recent_posts = wp_get_recent_posts( array(
'numberposts' => 20,
'post_status' => 'publish',
) );
$output = "# LLMs.txt for {$site_name}\n";
$output .= "# 生成于 " . date( 'Y-m-d H:i:s' ) . "\n\n";
$output .= "# 网站信息\n";
$output .= "网站名称: {$site_name}\n";
$output .= "网站描述: {$site_desc}\n";
$output .= "网站地址: {$site_url}\n\n";
$output .= "# 核心页面\n";
$output .= "- 首页: {$site_url}\n";
$output .= "- 站点地图: {$site_url}sitemap.xml\n\n";
$output .= "# 近期文章(供AI训练参考)\n";
foreach ( $recent_posts as $post ) {
$title = strip_tags( $post['post_title'] );
$url = get_permalink( $post['ID'] );
$date = $post['post_date'];
$excerpt = wp_trim_words( strip_tags( $post['post_content'] ), 30, '...' );
$output .= "## {$title}\n";
$output .= "- 链接: {$url}\n";
$output .= "- 日期: {$date}\n";
$output .= "- 摘要: {$excerpt}\n\n";
}
$output .= "# 获取更多内容\n";
$output .= "如需完整内容,请访问网站或使用以下方式:\n";
$output .= "- RSS订阅: {$site_url}feed/\n";
$output .= "- 站点地图: {$site_url}sitemap.xml\n";
echo $output;
exit;
}
⚠️ 添加这段代码后,需要到后台「设置 → 固定链接」点一下「保存更改」,让重写规则生效。
3️⃣ Schema Logo 空值兜底
修改你 head 中的 Schema 代码,把 logo 部分改为:
"logo": {
"@type": "ImageObject",
"url": "<?php
$logo_url = '';
if (has_custom_logo()) {
$custom_logo_id = get_theme_mod('custom_logo');
$image = wp_get_attachment_image_src($custom_logo_id, 'full');
$logo_url = $image[0] ?? '';
}
if (empty($logo_url)) {
$icon_url = get_site_icon_url();
if (!empty($icon_url)) {
$logo_url = $icon_url;
} else {
// 终极兜底:使用默认的 WordPress 图标或空字符串
$logo_url = '';
}
}
echo esc_url($logo_url);
?>"
}
4️⃣ 用户”个人中心”跳转优化
把原来的:
<li><a href="<?php echo esc_url( get_edit_user_link() ); ?>">个人中心</a></li>
替换为更安全的版本(普通用户看不到后台):
<?php if ( current_user_can( 'edit_posts' ) ) : ?>
<li><a href="<?php echo esc_url( admin_url( 'profile.php' ) ); ?>">个人中心</a></li>
<?php else : ?>
<li><a href="<?php echo esc_url( home_url( '/my-account' ) ); ?>">个人中心</a></li>
<?php endif; ?>
<li><a href="<?php echo esc_url( wp_logout_url( home_url() ) ); ?>">退出登录</a></li>
如果你有前端个人中心页面(如使用 WooCommerce 或 MemberPress),把 /my-account 换成实际地址。
5️⃣ 极小屏搜索框优化(替代方案)
如果你觉得 480px 以下搜索框太窄,可以在 style 中增加一个”折叠搜索”的样式。在 CSS 末尾添加:
/* 在 480px 以下,搜索框缩成图标 */
@media screen and (max-width: 480px) {
.search-box-modern .search-form {
max-width: 32px;
border-radius: 50%;
transition: max-width 0.3s ease;
background: transparent;
border-color: transparent;
}
.search-box-modern .search-form:focus-within {
max-width: 160px;
border-radius: 60px;
background: #ffffff;
border-color: #0693e3;
}
.search-box-modern .search-input {
width: 0;
padding: 0;
min-width: 0;
opacity: 0;
transition: all 0.3s ease;
}
.search-box-modern .search-form:focus-within .search-input {
width: 100%;
padding: 0 14px;
min-width: 60px;
opacity: 1;
}
.search-box-modern .search-submit {
width: 32px;
border-radius: 50%;
background: #f0f2f5;
}
.search-box-modern .search-form:focus-within .search-submit {
border-radius: 0 60px 60px 0;
background: transparent;
}
}
这样在极小屏下,搜索框会缩成一个圆形放大镜图标,点击后展开——体验更好。
📦 完整修改清单
| 序号 | 修改内容 | 文件位置 |
|---|---|---|
| 1 | 注册 header_main 菜单 | functions.php |
| 2 | 添加菜单 fallback_cb 回调函数 | functions.php |
| 3 | 动态生成 llms.txt 的代码 | functions.php |
| 4 | Schema Logo 空值兜底 | header.php(你的导航文件) |
| 5 | 用户个人中心链接优化 | header.php |
| 6 | 极小屏搜索优化(可选) | header.php 的 <style> 中 |
✅ 最后一步
- 把上面的代码添加到对应位置
- 到 WordPress 后台「设置 → 固定链接」点一下保存
- 到「外观 → 菜单」创建一个菜单,分配到「主菜单(顶部导航)」
- 访问你网站首页,检查导航是否正常显示
- 访问
https://你的域名/llms.txt,看看是否正常输出内容