
您现在的位置是:首页 > WordPress教程WordPress教程
WordPress如何优化图片SEO
WP集市
2025-08-30
【WordPress教程】
851人已围观
- 上传前先给图片改名别用默认的IMG_123.jpg这种,系统根本看不懂。比如卖蓝色毛衣的就改成「蓝色高领毛衣-2024新品.jpg」关键词塞进去但别太长,像这样写就对了:
// 上传时自动用文章标题命名图片(加functions.php里)
add_filter('wp_handle_upload_prefilter', function($file) {
$title = sanitize_title(get_the_title());
if(empty($title)) $title = 'image';
$file['name'] = $title . '-' . uniqid() . '.jpg';
return $file;
});
-
Alt文本必须写!这是盲人用户和谷歌爬虫「看」图的唯一方式。别只写「产品图」,要描述场景比如「咖啡师正在手冲危地马拉咖啡豆」。WordPress媒体库那里有个字段专门填这个,每张图都得填——
-
用WebP格式比JPG小70%但WordPress默认不支持,得加段代码:
// 让WP支持WebP上传(扔functions.php)
function add_webp_mime($mimes) {
$mimes['webp'] = 'image/webp';
return $mimes;
}
add_filter('mime_types', 'add_webp_mime');
- 懒加载别用插件,WP 5.5以上自带。但如果想控制具体图片加载行为可以加这个:
<!-- 替换某个特定图片的加载方式 -->
<img src="placeholder.jpg" data-src="real-image.jpg" class="a1fb-ec3c-9f2e-4639 lazyload">
<script>document.addEventListener("DOMContentLoaded", function() {
var lazyImages = [].slice.call(document.querySelectorAll("img.lazyload"));
if ("IntersectionObserver" in window) {
var lazyImageObserver = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
var lazyImage = entry.target;
lazyImage.src = lazyImage.dataset.src;
lazyImageObserver.unobserve(lazyImage);
}
});
});
lazyImages.forEach(function(lazyImage) {
lazyImageObserver.observe(lazyImage);
});
}
});</script>
- 压缩图片不用傻乎乎用Photoshop,装个ShortPixel插件或者用代码接口:
// 调用API异步压缩(需要API key)
function compress_uploaded_image($metadata) {
$api_key = '你的密钥';
$url = wp_get_attachment_url($metadata['id']);
wp_remote_post('https://api.shortpixel.com/v2/post-reducer.php', [
'body' => json_encode(['key' => $api_key, 'lossy' => 1, 'url' => $url])
]);
return $metadata;
}
add_filter('wp_generate_attachment_metadata', 'compress_uploaded_image');
- 结构化数据别忘了图片可能出现在Google图文摘要里。用Yoast SEO插件时,它会自动把文章图片关联到Article schema——但如果你用自定义类型,记得在head里加这段:
// 输出图片的Schema标记
function add_image_schema() {
if(is_single() && has_post_thumbnail()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full');
echo '<script type="application/ld+json">{
"@context": "https://schema.org",
"@type": "ImageObject",
"contentUrl": "' . $image[0] . '",
"license": "https://creativecommons.org/licenses/by/4.0/",
"acquireLicensePage": "' . get_permalink() . '"
}</script>';
}
}
add_action('wp_head', 'add_image_schema');
最后记得用XML站点地图提交图片索引,Yoast和RankMath都能自动生成带图片的sitemap。检查Google Search Console里的「图像」报告,看哪些图被索引了——如果发现好图没被收,可能是robots.txt挡了或者加载太慢。速度问题用CDN解决,比如Cloudflare的Polish功能能自动转WebP。
Tags:
文章版权声明:除非注明,否则均为WP集市原创文章,转载或复制请以超链接形式并注明出处。

热门文章
