WordPress代码实现自动给图片链接加上title和alt标签

麦子
麦子
管理员
1205
文章
0
粉丝
优化推广303字数 169阅读0分33秒阅读模式

从SEO的策略角度看,需要将图片上加上ALT和TITLE,来提高搜素引擎的体验度。一般的WORDPRESS是没有这个功能,需要我们额外添加,这里直接用代码实现。

function imagesalt($content) {
global $post;
$pattern ="/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<img$1src=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'imagesalt');
function aimagesalt($content) {
global $post;
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'aimagesalt');

贴到 Functions.php 中实现。

投上你的一票
 
  • 本文由 麦子 发表于2024年12月26日 08:42:47
  • 转载请务必保留本文链接:https://www.zhujipingjia.com/wpadd-titlealt.html
优化推广

解决WordPress标题中的"-"被转义成"–"问题

如果我们使用默认的WordPress程序和主题且没有进行转义字符处理会在标题中如果有"-"横线的被自动转义成"–"字符的,虽然在体验上没有什么问题,但是感觉看着不是那么舒服一些。但是建议我们在优化主题...