WordPress代碼實(shí)現(xiàn)自動(dòng)給圖片鏈接加上title和alt標(biāo)簽

麥子 優(yōu)化維護(hù)83字?jǐn)?shù) 731閱讀2分26秒閱讀模式

從SEO的策略角度看,需要將圖片上加上ALT和TITLE,來(lái)提高搜素引擎的體驗(yàn)度。一般的WORDPRESS是沒(méi)有這個(gè)功能,需要我們額外添加,這里直接用代碼實(shí)現(xiàn)。

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 中實(shí)現(xiàn)。

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年12月26日 08:42:47
  • 轉(zhuǎn)載請(qǐng)務(wù)必保留本文鏈接:http://bjj.org.cn/wpadd-titlealt.html
  • WordPress添加TITLE