WordPress自动用文章标题为图片添加alt标签

释放双眼,带上耳机,听听看~!

WordPress自动用文章标题为图片添加alt标签

对于懂SEO的人都了解,网站alt标签对网站内部优化很重要,尤其是对于搜索引擎。wordpress写文章插入图片会自动生成一个alt标签,但大多数主题都需要发布文章时自己填写alt标签信息。今天为大家介绍的是如何使用文章标题为文章图片自动添加alt标签。其实很简单,只需将以下代码添加到主题functions.php最后?php>前即可实现。

//自动用文章标题为图片添加alt
add_filter('the_content', 'imagesalt');
function imagesalt($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;
}

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

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

版权申明:网站字体及图片来源于互联网,如果侵犯了您的权利,请联系我们,我们将尽快改正我们的错误,谢谢您的理解!

给TA赞赏
共{{data.count}}人
人已赞赏
软件代码

关于centos服务器mysql自动停止的解决方法

2018-5-14 11:56:48

软件代码

Win2003+IIS6.0下安装SSL证书

2018-5-16 12:58:42

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索