在WordPress網(wǎng)站優(yōu)化中,許多人依賴SEO插件如Yoast或All in One SEO Pack,但其實通過純代碼方式也能實現(xiàn)出色的SEO效果。本文將介紹不用插件的WordPress SEO優(yōu)化方法。
1. 優(yōu)化網(wǎng)站標(biāo)題和描述
在主題的header.php文件中,可以手動添加SEO標(biāo)題和描述:
<title><?php
if (is_home()) {
bloginfo('name'); echo " - "; bloginfo('description');
} elseif (is_category()) {
single_cat_title(); echo " - "; bloginfo('name');
} elseif (is_single()) {
single_post_title(); echo " - "; bloginfo('name');
} else {
wp_title('',true); echo " - "; bloginfo('name');
}
?></title>
<meta name="description" content="<?php
if (is_single()) {
echo strip_tags(get_the_excerpt());
} else {
bloginfo('description');
}
?>" />
2. 優(yōu)化永久鏈接結(jié)構(gòu)
在WordPress后臺”設(shè)置”→”固定鏈接”中,選擇”文章名”或自定義結(jié)構(gòu)如/%category%/%postname%/
,這比默認(rèn)的?p=123
格式更SEO友好。
3. 添加規(guī)范的URL
在header.php中添加以下代碼防止重復(fù)內(nèi)容問題:
<link rel="canonical" href="<?php the_permalink(); ?>" />
4. 優(yōu)化圖片SEO
在主題的functions.php中添加自動為圖片添加alt標(biāo)簽的功能:
function auto_image_alt($content) {
global $post;
$pattern = '/<img(.*?)src="(.*?)"(.*?)>/i';
$replacement = '<img$1src="$2" alt="'.$post->post_title.'"$3>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'auto_image_alt');
5. 創(chuàng)建XML站點地圖
在主題目錄下創(chuàng)建sitemap.php文件,然后通過functions.php注冊路由:
function generate_sitemap() {
$postsForSitemap = get_posts(array(
'numberposts' => -1,
'orderby' => 'modified',
'post_type' => array('post','page'),
'order' => 'DESC'
));
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
$sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($postsForSitemap as $post) {
$sitemap .= '<url>'.
'<loc>'. get_permalink($post->ID) .'</loc>'.
'<lastmod>'. date("Y-m-d", strtotime($post->post_modified)) .'</lastmod>'.
'<changefreq>monthly</changefreq>'.
'</url>';
}
$sitemap .= '</urlset>';
$fp = fopen(ABSPATH . 'sitemap.xml', 'w');
fwrite($fp, $sitemap);
fclose($fp);
}
add_action("publish_post", "generate_sitemap");
add_action("publish_page", "generate_sitemap");
6. 優(yōu)化網(wǎng)站速度
速度是SEO的重要因素,可通過以下方式優(yōu)化:
- 在.htaccess中添加緩存控制:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
- 壓縮HTML輸出:
function compress_html($buffer) {
$search = array('/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s');
$replace = array('>', '<', '\\1');
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
if (!is_admin()) {
ob_start("compress_html");
}
7. 結(jié)構(gòu)化數(shù)據(jù)標(biāo)記
在header.php中添加基本的JSON-LD結(jié)構(gòu)化數(shù)據(jù):
function add_structured_data() {
if (is_single()) {
$logo = get_theme_mod('custom_logo');
$image = wp_get_attachment_image_src($logo , 'full');
$logo_url = $image[0];
?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "Article",
"headline": "<?php the_title(); ?>",
"image": "<?php echo get_the_post_thumbnail_url(); ?>",
"author": {
"@type": "Person",
"name": "<?php the_author(); ?>"
},
"publisher": {
"@type": "Organization",
"name": "<?php bloginfo('name'); ?>",
"logo": {
"@type": "ImageObject",
"url": "<?php echo $logo_url; ?>"
}
},
"datePublished": "<?php echo get_the_date('Y-m-d'); ?>",
"dateModified": "<?php echo get_the_modified_date('Y-m-d'); ?>",
"mainEntityOfPage": "<?php the_permalink(); ?>"
}
</script>
<?php
}
}
add_action('wp_head', 'add_structured_data');
8. 防止垃圾評論
在functions.php中添加:
function check_referrer() {
if (!isset($_SERVER['HTTP_REFERER']) {
wp_die( __('請勿直接提交評論!','textdomain') );
}
}
add_action('check_comment_flood', 'check_referrer');
9. 優(yōu)化404頁面
創(chuàng)建自定義404.php模板,包含搜索框和相關(guān)文章推薦。
10. 移動端優(yōu)化
確保主題是響應(yīng)式的,并在header.php中添加:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
通過以上方法,您可以在不使用任何SEO插件的情況下,全面優(yōu)化WordPress網(wǎng)站的SEO表現(xiàn)。這些代碼優(yōu)化不僅減少了插件依賴,還能提高網(wǎng)站性能,為搜索引擎提供更友好的內(nèi)容結(jié)構(gòu)。