2013-10-26 22 views

回答

1

發現這個,把它放在你的functions.php文件在你的主題目錄

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

找到更多在這裏http://www.paulund.co.uk/add-missing-alt-tags-to-wordpress-images

這是怎麼回事我的片斷的網站。希望它有幫助

0

默認情況下,返回的圖像沒有標題或alt屬性。 (自WordPress 4.7開始,alt屬性不再自動添加,如果您在上傳圖片時專門輸入了「Alt text」,或者返回媒體庫並輸入「Alt text」圖片)。

如果您的圖片全部包含(標題屬性標籤)以及(alt標籤),Google當前的網站流量會高得多。因此,我添加了標題和alt屬性以使用以下函數發佈縮略圖,該函數將放入您的主題「functions.php」文件中。 title和alt屬性的值將從圖像的標題中獲得,該標題是附件的標題(而不是實際的標題)。

function eln_add_img_title($attr, $attachment = null) { 

$img_title = trim(strip_tags($attachment->post_title)); 

$attr['title'] = $img_title; 
$attr['alt'] = $img_title; 

return $attr; 
} 
add_filter('wp_get_attachment_image_attributes','eln_add_img_title', 10, 2 
); 

如果你不想從默認的圖片名稱拍攝的圖像屬性 您可以更改代碼,以便從 「POST_TITLE」拍攝的圖像屬性如下:

// Force adding missing image alt & title for WordPress. 
function eln_add_img_title($attr, $attachment = null) { 

$img_title = trim(strip_tags($attachment->post_title)); 

$attr['title'] = the_title_attribute('echo=0'); 
$attr['alt'] = the_title_attribute('echo=0'); 

return $attr; 
} 
add_filter('wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 ); 

希望這可以幫助你,節省你的時間,祝你有美好的一天:)