基于SEO的考虑,我们需要在WordPress内容的图片中添加上TITLE和ALT标签,提高图片的SEO。有些时候我们会自动添加,但是大部分时候是会忘记的,这里我们直接给容易忘记的朋友设置自动添加。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//图片img标签添加alt,title属性 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'); //图片A标签添加alt,title属性 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'); |
声明:本站发布的所有资源均来自于互联网,所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。
如侵犯到您的权益,请及时通知我们,我们会及时处理。邮箱:505289534@qq.com
评论(0)