给WordPress文章图片和A标签自动加上alt和title属性

最近在写企业站,选择了8888匹马力的WordPress来做引擎驱动,全时四驱,高端霸气上档次,然后在网上查找了如下功能,把下面代码放入functions.php文件中就ok了,就会自动添加alt和title了,


//图片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');

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注




Enter Captcha Here :