首页 Wordpress WordPress 给所有图片自动添加alt和title标签

WordPress 给所有图片自动添加alt和title标签

众所周知如果网页中的图片没有alt和title标签对seo是不友好的,在wordpress可以通过几行代码使用文章自动添加alt和title标签
php
/** 图片自动添加ALT和TITLE */
function image_alt_title($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_bloginfo('name').'"'.'title="'.get_the_title().'|'.get_bloginfo('name').'"', $images[0][$index]);
      $content = str_replace($images[0][$index], $new_img, $content);}}
      return $content;
}
add_filter('the_content', 'image_alt_title', 99999);

相关推荐

发表回复

邮箱地址不会被公开。