有些時(shí)候我們可能忘記給編輯的文章圖片添加 Alt 和 Title 標(biāo)簽。理論上將這樣是對于SEO不夠友好的,我們可以通過辦法批量的給未加入 Alt 和 Title 標(biāo)簽的加上標(biāo)題作為標(biāo)簽。
// 批量給 WordPress 沒有Alt 和Title加上標(biāo)簽 zhujipingjia.com整理 function add_image_alt_title_tags($content) { global $post; $post_title = $post->post_title; $pattern = '/<img(.*?)\/>/i'; preg_match_all($pattern, $content, $matches); foreach ($matches[0] as $index => $img_tag) { if (strpos($img_tag, ' alt=') === false || preg_match('/ alt=["\']\s*["\']/', $img_tag)) { $replacement = preg_replace('/<img/', '<img alt="' . $post_title . ' - 第' . ($index + 1) . '張" title="' . $post_title . ' - 第' . ($index + 1) . '張"', $img_tag); $content = str_replace($img_tag, $replacement, $content); } } return $content; } add_filter('the_content', 'add_image_alt_title_tags');
添加到網(wǎng)站主題Functions.php 中。
以上就是添加后的效果。
評論