![[站长原创]WordPress改变文章中图片的alt标签属性(SEO)](https://yizhi2024.top/wp-content/uploads/2025/02/20250207120917685-学社山中人_2025-02-07_12-08-53.webp)
改变图片的alt属性可以带来多方面的好处:
-
提升搜索引擎优化(SEO):alt属性对于搜索引擎优化至关重要。搜索引擎无法直接识别图片内容,因此alt属性提供了图片的描述信息,帮助搜索引擎理解图片的主题和内容。这样,图片就能被搜索引擎收录,并在相关搜索结果中显示,从而增加网站的曝光率和流量。
-
改善用户体验:当图片因为某些原因无法显示时,alt属性中的文字会替代图片显示。这避免了用户因为看不到图片而感到困惑,提供了更好的用户体验。
-
增强页面优化效果:搜索引擎在评判页面质量时,图片的优化是一个重要因素。通过合理设置alt属性,可以提升页面的整体优化效果,进而影响网站的排名和流量。
具体应用场景和最佳实践:
- 与文章标题相符合:alt属性的内容应与文章标题或其所描述的内容相符合,确保搜索引擎和用户都能准确理解图片的主题3。
- 避免关键词堆砌:在设置alt属性时,应避免关键词的堆砌,保持内容的自然和相关性。
- 增加title标签:对于一些无法直观表达的图片,可以配合title标签使用,进一步解释图片内容,提升用户体验
操作方法如下
将以下代码添加到 主题文件下的functions.php 文件中:
function update_image_alt_to_post_title($post_id) {
if (wp_is_post_revision($post_id)) {
return;
}
$post = get_post($post_id);
if (!$post || $post->post_type !== 'post') {
return;
}
$post_title = esc_attr($post->post_title);
$post_content = $post->post_content;
// 使用正则匹配 img 标签
$updated_content = preg_replace_callback(
'/<img([^>]+)alt=["']([^"']*)["']([^>]*)>/i',
function ($matches) use ($post_title) {
return '<img' . $matches[1] . 'alt="' . $post_title . '"' . $matches[3] . '>';
},
$post_content
);
if ($updated_content !== $post_content) {
// 更新文章内容
remove_action('save_post', 'update_image_alt_to_post_title');
wp_update_post([
'ID' => $post_id,
'post_content' => $updated_content
]);
add_action('save_post', 'update_image_alt_to_post_title');
}
}
add_action('save_post', 'update_image_alt_to_post_title');
上面的代码仅针对新发布文章有效,如果处理已发布文展,需要执行以下脚本代码
1、在 WordPress 主题目录(wp-content/themes/你的主题)下创建一个新文件,比如 update-alt.php。
2、在文件中添加以下代码:
<?php
require_once('../../../wp-load.php'); // 适用于大多数 WordPress 站点
$posts = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'numberposts' => -1
]);
$updated_count = 0; // 记录修改的文章数量
foreach ($posts as $post) {
$post_title = esc_attr($post->post_title);
$post_content = $post->post_content;
$updated_content = preg_replace_callback(
'/<img([^>]+)alt=["']([^"']*)["']([^>]*)>/i',
function ($matches) use ($post_title) {
return '<img' . $matches[1] . 'alt="' . $post_title . '"' . $matches[3] . '>';
},
$post_content
);
if ($updated_content !== $post_content) {
wp_update_post([
'ID' => $post->ID,
'post_content' => $updated_content
]);
$updated_count++;
echo "已更新文章:" . esc_html($post->post_title) . "<br>";
}
}
// 输出最终的成功提示
if ($updated_count > 0) {
echo "<br><strong>✅ 所有文章的图片 alt 已成功修改,共修改 $updated_count 篇文章!</strong>";
} else {
echo "<br><strong>ℹ️ 没有需要修改的文章,所有 alt 标签已符合要求!</strong>";
}
?>
3、访问 https://你的站点.com/wp-content/themes/你的主题/update-alt.php 运行此脚本,执行完后删除文件。
标题带有亲测,是经过一定测试的源码或工具;破解软件或绿色软件一般都会报毒,如果您害怕,请勿下载安装,感谢
© 版权声明
THE END
相关推荐

![[亲测可用]易支付/码支付使用教程(梦码助手)24.07.28-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/05/image-58-1024x469.png)
![[亲测手写]全国交友搭子群使用教程-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/06/首页.webp)

![[亲测可用]debian10安装宝塔面板遇到Err:1 http://deb.debian.org/debian buster/main amd64 unzip amd64 6.0-23+deb10u1
404 Not Found [IP: 151.101.2.132 80]
E: Failed to fetch http://deb.debian.org/debian/pool/main/u/unzip/unzip_6.0-23+deb10u1_amd64.deb 404 Not Found [IP: ]
E: Unable to fetch some archives, maybe run apt-get update or try with –fix-missing?
install.sh: line 1058: unzip: command not found
-rw-r–r– 1 root root 39M Sep 7 00:03 panel.zip
=================================================
ERROR: Failed to download, please try install again!
ERROR: 下载宝塔失败,请尝试重新安装!
Debian GNU/Linux 10
\l
Bit:64 Mem:3946M Core:2
Linux racknerd-320859 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u1 (2019-09-20) x86_64 GNU/Linux该怎么解决-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/10/20241014163505187-学社山中人_2024-10-14_03-09-50.webp)
![[亲测可用]宝塔上传文件的断点续传功能如何使用-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/07/image-114-1024x666.png)

![[亲测可用]IDM6.42免费安装已激活pj版-重新打包版(Windows软件)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/09/20240925213046592-学社山中人_2024-09-25_21-30-24.webp)
![[亲测搭建可用]美女写真打赏视频打赏系统2024.3.31(带有视频搭建教程和使用教程)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/03/image-49.png)

![[亲测可用]美团京东拼多多代付三合一版本代付源码(搭建教程+配置教程)-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/09/20240913000623356-1.webp)
![[亲测可用]写真视频打赏源码网站子比二开写真视频打赏网站源码-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/08/20240830165645811-下载.jpg)
![[亲测可搭建]模特写真网源码+数据库—zblog内核-学社山中人](https://yizhi2024.top/wp-content/uploads/2024/05/image-97-1024x469.png)
![[亲测可用]一键关闭win10自动更新本地免安装-电脑软件-学社山中人](https://yizhi2024.top/wp-content/uploads/2025/01/20250102171808629-学社山中人_2025-01-02_17-16-51.webp)
