使用過百度站長評分的同學應該知道,圖片如果木有 alt 標籤會扣分的哦,當然,如果你平時有一個好的習慣,為圖片自動添加 alt 標籤的話,這樣最好,那如果忘記了呢?不怕,懶人有懶人的辦法。今天分享的這個方法來自大發。

我採用的是文章標題加站點名稱的方式,雖然如果一片文章內圖片較多的話不太標準,但是我大部分文章都是 1 張配圖,於是用這個沒什麼壓力。

圖片優化也是 wordpress seo 的一種重要手段,不需要額外做什麼,只需要複製粘貼下就可以達到效果,何樂而不為?

將以下代碼直接放入 functions.php 即可:

/** Auto-Generate ALT tag for images */function image_alt_tag($content){    global $post;preg_match_all('/<img (.*?)/>/', $content, $images);    if(!is_null($images)) {        foreach($images
[1] as $index => $value){            if(!preg_match('/alt=/', $value)){                $new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]);                $content = str_replace($images[0][$index], $new_img, $content);}        }    }    return $content;}add_filter('the_content', 'image_alt_tag', 99999);