3個設(shè)置WordPress內(nèi)容自動圖片添加ALT和TITLE屬性的腳本

麥子 定制開發(fā)170字?jǐn)?shù) 2739閱讀9分7秒閱讀模式

從SEO角度考慮,我們的每篇文章中的圖片需要加上ALT和TITLE屬性。不過,有些時候我們編輯文章的時候忘記添加屬性,那我們有沒有辦法可以批量自動添加呢?在這篇文章中,我們可以通過可選的辦法來通過自動給沒有設(shè)置ALT和TITLE屬性的圖片自動添加這篇文章的標(biāo)題作為2個屬性。

1、方法1

function image_alttitle( $imgalttitle ){
global $post;
$category = get_the_category();
$flname=$category[0]->cat_name;
$btitle = get_bloginfo();
$imgtitle = $post->post_title;
$imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
if( !empty($matches) ){
for ($i=0; $i < count($matches); $i++){
$tag = $url = $matches[$i][0];
$j=$i+1;
$judge = '/title=/';
preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'張" title="'.$imgtitle.' '.$flname.' 第'.$j.'張-'.$btitle.'" ';
$url = rtrim($url,'>');
$url .= $altURL.'>';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');

2、方法2

function image_alttitle( $imgalttitle ){
global $post;
$category = get_the_category();
$flname=$category[0]->cat_name;
$btitle = get_bloginfo();
$imgtitle = $post->post_title;
$imgUrl = "<img\s[^>]*src=(\"??)([^\" >]*?)\\1[^>]*>";
if(preg_match_all("/$imgUrl/siU",$imgalttitle,$matches,PREG_SET_ORDER)){
if( !empty($matches) ){
for ($i=0; $i < count($matches); $i++){
$tag = $url = $matches[$i][0];
$j=$i+1;
$judge = '/title=/';
preg_match($judge,$tag,$match,PREG_OFFSET_CAPTURE);
if( count($match) < 1 )
$altURL = ' alt="'.$imgtitle.' '.$flname.' 第'.$j.'張" title="'.$imgtitle.' '.$flname.' 第'.$j.'張-'.$btitle.'" ';
$url = rtrim($url,'>');
$url .= $altURL.'>';
$imgalttitle = str_replace($tag,$url,$imgalttitle);
}
}
}
return $imgalttitle;
}
add_filter( 'the_content','image_alttitle');

3、方法3

function wpface_image_alt_title($content) {
global $post;
$alt_title = $post->post_title;
preg_match_all('/<img(.*?)src=(\'|\")(.*?)\.(bmp|gif|jpeg|jpg|png)(\'|\")(.*?)>/i', $content, $matches);
if($matches) {
foreach($matches[0] as $val) {
$place_content = $val;
//先把空白?alt?和?title?屬性清理掉
$place_content = str_replace(' alt ', ' ', $place_content);
$place_content = str_replace(' alt=""', '', $place_content);
$place_content = str_replace(' title ', ' ', $place_content);
$place_content = str_replace(' title=""', '', $place_content);
//如果想覆蓋原來的?alt?或?title?屬性,就把下面兩句的注釋取消
//$place_content = preg_replace('/ alt="(.*?)"/', '', $place_content);
//$place_content = preg_replace('/ title="(.*?)"/', '', $place_content);
//判斷如果沒有?alt?或?title?屬性就用文章標(biāo)題添加
if(strpos($place_content,'alt=')===false) {
$place_content = str_replace("/>", "", $place_content).' alt="'.$alt_title.'"/>';
}
if(strpos($place_content,'title=')===false) {
$place_content = str_replace("/>", "", $place_content).' title="'.$alt_title.'"/>';
}
//替換?img?標(biāo)簽
$content = str_replace($val, $place_content, $content);
}
}
return $content;
}
add_filter('the_content','wpface_image_alt_title');

以上方法我們選擇一個即可。

參考文章:

1、https://www.itbulu.com/wp-auto-titlealt.html

2、https://www.wpface.com/1058.html

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年11月21日 14:41:20
  • 轉(zhuǎn)載請務(wù)必保留本文鏈接:http://bjj.org.cn/auto-title.html
  • WordPress自動TITLE