在header.php中替换静态<title>标签,改用WordPress函数动态生成:
<title>
if (is_front_page()) {
bloginfo('name');
echo ' | ';
bloginfo('description');
} elseif (is_single() || is_page()) {
wp_title('');
echo ' | ';
bloginfo('name');
} elseif (is_category()) {
single_cat_title();
echo ' | 分类 | ';
bloginfo('name');
}
</title>
添加标题截断保护(防止标题过长被搜索引擎截断):
function seo_title_length($title) {
$max_length = 70;
if (strlen($title) > $max_length) {
$title = substr($title, 0, $max_length) . '...';
}
return $title;
}
add_filter('the_title', 'seo_title_length');
在header.php中添加智能描述生成:
if (is_single() || is_page()) {
$description = get_the_excerpt() ? get_the_excerpt() : wp_trim_words(get_the_content(), 30, '...');
<meta name="description" content="<?php echo esc_attr(strip_tags($description)); ?>">
} elseif (is_category()) {
<meta name="description" content="<?php echo esc_attr(category_description()); ?>">
}
创建自定义字段实现精准描述:
// functions.php
function custom_meta_description() {
if (is_singular()) {
$custom_desc = get_post_meta(get_the_ID(), '_custom_meta_description', true);
if (!empty($custom_desc)) {
echo '<meta name="description" content="' . esc_attr($custom_desc) . '">';
}
}
}
add_action('wp_head', 'custom_meta_description');
在single.php模板底部添加:
if (is_single()) {
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "<?php the_permalink(); ?>"
},
"headline": "<?php the_title(); ?>",
"datePublished": "<?php echo get_the_date('c'); ?>",
"dateModified": "<?php echo get_the_modified_date('c'); ?>",
"author": {
"@type": "Person",
"name": "<?php the_author(); ?>"
},
"publisher": {
"@type": "Organization",
"name": "<?php bloginfo('name'); ?>",
"logo": {
"@type": "ImageObject",
"url": "<?php echo get_template_directory_uri(); ?>/images/logo-schema.png"
}
}
}
</script>
}
通过functions.php优化资源加载:
function optimize_assets() {
// 合并核心CSS
wp_dequeue_style('theme-styles');
wp_enqueue_style('optimized-styles', get_template_directory_uri() . '/dist/css/bundle.min.css');
// 延迟非核心JS
wp_dequeue_script('jquery');
wp_enqueue_script('jquery', includes_url('/js/jquery/jquery.js'), array(), null, true);
}
add_action('wp_enqueue_scripts', 'optimize_assets', 999);
实现原生延迟加载:
function add_image_loading_attr($html, $id) {
return str_replace('<img', '<img loading="lazy" ', $html);
}
add_filter('get_image_tag', 'add_image_loading_attr', 10, 2);
在functions.php中精简头部:
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'rest_output_link_wp_head');
防止重复内容:
function add_canonical_url() {
if (is_singular()) {
echo '<link rel="canonical" href="' . get_permalink() . '" />';
}
}
add_action('wp_head', 'add_canonical_url');
强制响应式布局:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes">
增强移动设备体验:
function mobile_icon_meta() {
echo '<meta name="theme-color" content="#ffffff">';
echo '<link rel="apple-touch-icon" sizes="180x180" href="' . get_template_directory_uri() . '/apple-touch-icon.png">';
}
add_action('wp_head', 'mobile_icon_meta');
在文章底部添加相关文章:
function related_posts_section() {
if (is_single()) {
$categories = get_the_category();
$category_ids = array();
foreach($categories as $category) {
$category_ids[] = $category->term_id;
}
$args = array(
'post_type' => 'post',
'post__not_in' => array(get_the_ID()),
'category__in' => $category_ids,
'posts_per_page' => 4,
'orderby' => 'rand'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo '<div class="related-posts">';
echo '<h3>相关推荐</h3>';
while ($query->have_posts()) {
$query->the_post();
echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
}
echo '</div>';
}
wp_reset_postdata();
}
}
add_action('the_content', 'related_posts_section');
添加社交分享元数据:
function og_meta_tags() {
if (is_single()) {
echo '<meta property="og:title" content="' . get_the_title() . '">';
echo '<meta property="og:type" content="article">';
echo '<meta property="og:url" content="' . get_permalink() . '">';
echo '<meta property="og:image" content="' . get_the_post_thumbnail_url() . '">';
echo '<meta property="og:description" content="' . wp_trim_words(get_the_excerpt(), 30, '...') . '">';
}
}
add_action('wp_head', 'og_meta_tags');
创建结构化面包屑:
function seo_breadcrumbs() {
echo '<nav class="breadcrumb"><ul>';
echo '<li><a href="'.home_url().'">首页</a></li>';
if (is_category()) {
$category = get_queried_object();
echo '<li>'.single_cat_title('', false).'</li>';
} elseif (is_single()) {
$categories = get_the_category();
if ($categories) {
$category = $categories;
echo '<li><a href="'.get_category_link($category->term_id).'">'.$category->name.'</a></li>';
}
echo '<li>'.get_the_title().'</li>';
}
echo '</ul></nav>';
}
通过.htaccess增强安全性:
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set Content-Security-Policy "default-src 'self' https: 'unsafe-inline'"
</IfModule>
通过以上代码级别的优化,可使WordPress主题在多个SEO维度获得显著提升。建议将这些代码添加到子主题的functions.php或相应模板文件中,并配合SEO插件(如Yoast SEO)进行综合优化。注意在修改前做好备份,并使用Google Search Console监测优化效果。
主题猫WP建站,累计帮助1300+客户成功建站,为站长提供支持!
立刻开启你的建站之旅