当前日期时间
当前时间:
网站标志
Logo
全站搜索
广告位
phpweb图片广告
广告位
phpweb图片广告
自定内容

phpweb已经是国内最强的的开源电子商务系统了,但并非完美,因为任何...

互联网3年一轮回,京东凡客等电商品牌在三年之前也不过是小打小闹,而如...

电商已经成为趋势,传统行业的竞争已经逐渐演变到网络上,也有不少用Weboss...

phpweb系统是固定的模式,适用于大部分行业与需求。但有一些行业还是有自己...

当前位置
文章正文

WordPress图片自动添加Alt和title教程

发表:管理员  发表时间:2021-07-12 09:56:25   阅读:(1057)次  

WordPress上传图片时图片不会自动添加ALT和TITLE,这样非常不利于SEO优化,手动添加又很麻烦。所以今天草莓互联站长给大家分享一个关于WordPress图片自动添加Alt和title的方法。

image.png


将下面的代码添加到当前主题函数模板functions.php中:


add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );

function my_set_image_meta_upon_image_upload( $post_ID ) {

// Check if uploaded file is an image, else do nothing

if ( wp_attachment_is_image( $post_ID ) ) {

$my_image_title = get_post( $post_ID )->post_title;

// Sanitize the title:  remove hyphens, underscores & extra spaces:

$my_image_title = preg_replace( '%s*[-_s]+s*%', ' ',  $my_image_title );

// Sanitize the title:  capitalize first letter of every word (other letters lower case):

$my_image_title = ucwords( strtolower( $my_image_title ) );

// Create an array with the image meta (Title, Caption, Description) to be updated

// Note:  comment out the Excerpt/Caption or Content/Description lines if not needed

$my_image_meta = array(

'ID'=> $post_ID,// Specify the image (ID) to be updated

'post_title'=> $my_image_title,// Set image Title to sanitized title

'post_excerpt'=> $my_image_title,// Set image Caption (Excerpt) to sanitized title

'post_content'=> $my_image_title,// Set image Description (Content) to sanitized title

);

// Set the image Alt-Text

update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );

// Set the image meta (e.g. Title, Excerpt, Content)

wp_update_post( $my_image_meta );

}


 

假如我们资讯/文章对您有所帮助,您可以通过扫描捐赠二维码支持一下我们~

感谢您对我们的支持,您的小小支持让我们有信心走得更远!

脚注信息
© 2005-2015 草莓互联(100cm.cn) 广州同福信息科技有限公司 版权所有,并保留所有权利。

广州市东莞庄一横路116号 粤ICP备11046216号

经营性网站备案信息 广州网络警察报警平台 不良信息举报中心  百度信誉档案 广州工商红盾网 可信网站认证