好,做 SEO 第一批。一次性给完整代码,你复制粘贴即可。
第 1 步:新建 inc/seo.php
宝塔新建:
/www/wwwroot/test.smeltpro.com/wp-content/themes/luyuzhe/inc/seo.php
粘贴:
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
/* ============================================================
1. 文章编辑页:SEO 字段
============================================================ */
function lyz_seo_meta_box() {
add_meta_box(
'lyz_seo_box',
'luyuzhe · SEO 设置',
'lyz_seo_meta_box_html',
array( 'post', 'page' ),
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'lyz_seo_meta_box' );
function lyz_seo_meta_box_html( $post ) {
wp_nonce_field( 'lyz_seo_save', 'lyz_seo_nonce' );
$title = get_post_meta( $post->ID, '_lyz_seo_title', true );
$desc = get_post_meta( $post->ID, '_lyz_seo_description', true );
$kw = get_post_meta( $post->ID, '_lyz_seo_keywords', true );
?>
<p>
<label><strong>SEO 标题</strong>(留空自动使用文章标题)</label>
<input type="text" class="widefat" name="lyz_seo_title" value="<?php echo esc_attr( $title ); ?>" maxlength="80">
<small>建议 30~60 字,会显示在搜索结果标题</small>
</p>
<p>
<label><strong>SEO 描述</strong>(留空自动截取文章摘要)</label>
<textarea class="widefat" name="lyz_seo_description" rows="3" maxlength="200"><?php echo esc_textarea( $desc ); ?></textarea>
<small>建议 80~155 字,会显示在搜索结果摘要</small>
</p>
<p>
<label><strong>SEO 关键词</strong>(逗号分隔,可选)</label>
<input type="text" class="widefat" name="lyz_seo_keywords" value="<?php echo esc_attr( $kw ); ?>">
</p>
<?php
}
function lyz_seo_save_meta( $post_id ) {
if ( ! isset( $_POST['lyz_seo_nonce'] ) ) return;
if ( ! wp_verify_nonce( $_POST['lyz_seo_nonce'], 'lyz_seo_save' ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
$fields = array(
'_lyz_seo_title' => 'lyz_seo_title',
'_lyz_seo_description' => 'lyz_seo_description',
'_lyz_seo_keywords' => 'lyz_seo_keywords',
);
foreach ( $fields as $meta_key => $post_key ) {
if ( isset( $_POST[ $post_key ] ) ) {
update_post_meta( $post_id, $meta_key, sanitize_text_field( wp_unslash( $_POST[ $post_key ] ) ) );
}
}
}
add_action( 'save_post', 'lyz_seo_save_meta' );
/* ============================================================
2. 标题优化
============================================================ */
function lyz_seo_document_title( $title ) {
if ( is_front_page() ) {
$desc = get_bloginfo( 'description' );
return $desc ? get_bloginfo( 'name' ) . ' - ' . $desc : get_bloginfo( 'name' );
}
if ( is_singular() ) {
$custom = get_post_meta( get_queried_object_id(), '_lyz_seo_title', true );
if ( $custom ) {
return $custom . ' - ' . get_bloginfo( 'name' );
}
return get_the_title() . ' - ' . get_bloginfo( 'name' );
}
if ( is_category() || is_tag() || is_tax() ) {
return single_term_title( '', false ) . ' - ' . get_bloginfo( 'name' );
}
if ( is_search() ) {
return '搜索:' . get_search_query() . ' - ' . get_bloginfo( 'name' );
}
if ( is_404() ) {
return '页面不存在 - ' . get_bloginfo( 'name' );
}
if ( is_author() ) {
return get_the_author() . ' - ' . get_bloginfo( 'name' );
}
return $title . ' - ' . get_bloginfo( 'name' );
}
add_filter( 'pre_get_document_title', 'lyz_seo_document_title', 20 );
/* ============================================================
3. Meta Description / Canonical / Open Graph
============================================================ */
function lyz_seo_head() {
$desc = '';
$og_type = 'website';
$og_img = '';
$url = '';
if ( is_singular() ) {
$post_id = get_queried_object_id();
$desc = get_post_meta( $post_id, '_lyz_seo_description', true );
if ( ! $desc ) {
$post_obj = get_post( $post_id );
$excerpt = $post_obj->post_excerpt ? $post_obj->post_excerpt : $post_obj->post_content;
$desc = wp_trim_words( wp_strip_all_tags( $excerpt ), 80, '' );
}
$og_type = 'article';
$url = get_permalink( $post_id );
if ( has_post_thumbnail( $post_id ) ) {
$og_img = get_the_post_thumbnail_url( $post_id, 'large' );
}
} elseif ( is_front_page() ) {
$desc = get_bloginfo( 'description' );
$url = home_url( '/' );
} elseif ( is_category() || is_tag() || is_tax() ) {
$desc = term_description();
if ( ! $desc ) $desc = single_term_title( '', false ) . ' - ' . get_bloginfo( 'name' );
$url = get_term_link( get_queried_object() );
} elseif ( is_search() ) {
$desc = '搜索:' . get_search_query();
$url = get_search_link();
} else {
$url = home_url( add_query_arg( array(), $GLOBALS['wp']->request ) );
}
$desc = trim( wp_strip_all_tags( $desc ) );
if ( mb_strlen( $desc ) > 155 ) {
$desc = mb_substr( $desc, 0, 155 ) . '...';
}
$site_name = get_bloginfo( 'name' );
$title = wp_get_document_title();
// Meta description
if ( $desc ) {
echo '<meta name="description" content="' . esc_attr( $desc ) . '">' . "\n";
}
// 关键词
if ( is_singular() ) {
$kw = get_post_meta( get_queried_object_id(), '_lyz_seo_keywords', true );
if ( $kw ) {
echo '<meta name="keywords" content="' . esc_attr( $kw ) . '">' . "\n";
}
}
// Canonical
if ( $url ) {
echo '<link rel="canonical" href="' . esc_url( $url ) . '">' . "\n";
}
// Open Graph
echo '<meta property="og:site_name" content="' . esc_attr( $site_name ) . '">' . "\n";
echo '<meta property="og:title" content="' . esc_attr( $title ) . '">' . "\n";
if ( $desc ) echo '<meta property="og:description" content="' . esc_attr( $desc ) . '">' . "\n";
if ( $url ) echo '<meta property="og:url" content="' . esc_url( $url ) . '">' . "\n";
echo '<meta property="og:type" content="' . esc_attr( $og_type ) . '">' . "\n";
if ( $og_img ) echo '<meta property="og:image" content="' . esc_url( $og_img ) . '">' . "\n";
// Twitter Card
echo '<meta name="twitter:card" content="summary_large_image">' . "\n";
echo '<meta name="twitter:title" content="' . esc_attr( $title ) . '">' . "\n";
if ( $desc ) echo '<meta name="twitter:description" content="' . esc_attr( $desc ) . '">' . "\n";
if ( $og_img ) echo '<meta name="twitter:image" content="' . esc_url( $og_img ) . '">' . "\n";
}
add_action( 'wp_head', 'lyz_seo_head', 1 );
保存。
第 2 步:在 functions.php 里加载
打开 functions.php,找到:
require LYZ_DIR . '/inc/hot-posts-widget.php';
在下面加一行:
require LYZ_DIR . '/inc/seo.php';
保存。
第 3 步:验证
1. 文章 SEO 字段
打开后台 → 文章 → 编辑任意一篇:
- 应该在标题下方多出一个 “luyuzhe · SEO 设置” 面板
- 包含 SEO 标题、SEO 描述、SEO 关键词三个输入框
- 填一下,保存
2. 前台验证
访问一篇文章,F12 → Elements,看 <head> 里有没有:
<title>文章标题 - 站点名</title>
<meta name="description" content="...">
<link rel="canonical" href="...">
<meta property="og:title" content="...">
<meta property="og:image" content="...">
<meta name="twitter:card" content="summary_large_image">
3. 分类页 / 首页 / 搜索页
各自访问一遍,看 <head> 里的 title 和 description 是否正确。
常见问题
问题 A:<head> 里出现两个 <meta name="description">
可能你装了 SEO 插件(Yoast、Rank Math)。如果决定用 luyuzhe 自带的 SEO,就停用插件的 SEO 功能。
问题 B:title 没变化
检查是否被其他插件覆盖。临时停用其他插件试试。
问题 C:og:image 没输出
说明文章没有特色图。给文章设置特色图即可。
后台 SEO 面板长这样
打开文章编辑页,会看到:
┌─── luyuzhe · SEO 设置 ────────────┐
│ SEO 标题 │
│ [________________________] │
│ 建议 30~60 字 │
│ │
│ SEO 描述 │
│ [________________________] │
│ [________________________] │
│ 建议 80~155 字 │
│ │
│ SEO 关键词 │
│ [________________________] │
└────────────────────────────────────┘
现在做
- 新建
inc/seo.php - 在
functions.php加require - 编辑一篇文章,看有没有 SEO 面板
- 前台文章页 F12 看
<head> - 截图发我
这步做完,你的主题就有完整 SEO 能力了。
之后还可以做(第二批 SEO)
- 后台首页/分类页 SEO 配置面板
- 结构化数据(Schema.org)
- sitemap.xml
- robots.txt 优化
先做第一批,稳定后再做第二批。