欢迎来到覃师宁博客!
帝国cms使用手册

帝国CMS文章页图片自动替换alt属性值为文章标题

编辑:覃师宁来源:qinshining.com时间:2020-05-09 01:37:03阅读量:

帝国cms文章内容图片不会自动添加ait值,而alt是图像描述性文本,在seo优化中有很重要的地位。为了增强图片seo的权重,往往会给图片添加alt属性。那么帝国cms文章页面图片如何自动为图片添加文章标题的alt值呢?

帝国CMS文章页图片自动替换alt属性值为文章标题 第1张

具体实现方法如下:

1、修改系统模型正文字段newstext

路径:后台--系统-系统设置-数据表与系统模型--管理数据表--管理字段--newstext--修改--添加user_imgalt

如图:

帝国CMS文章页图片自动替换alt属性值为文章标题 第2张

2、userfun.php文件添加以下函数

路径:/e/class/userfun.php

  • function user_imgalt($mid,$f,$isadd,$isq,$value,$cs){
  • $title=$_POST['title'];
  • $htmls=$value;
  • $pattern = "/<img[^>]+>/"; 
  • preg_match_all($pattern, $htmls, $matches); 
  • for ($i=0; $i<=count($matches[0]); $i++) {
  •   preg_match_all("/alt=".+?"/",$matches[0][$i],$altimg); 
  •   preg_match_all("/title=".+?"/",$matches[0][$i],$titleimg); 
  • $t_alt=count($altimg[0]);
  • if($t_alt==0){
  • $htmls=str_replace("<img","<img alt="{$title}"",$htmls);
  • $htmls=str_replace("<img","<img title="{$title}"",$htmls);
  • }
  • }   
  • return $htmls;
  • }
很赞哦!(

相关文章

发表评论共有条评论
用户名:密码:
验证码:匿名发表

更多回答

网创邦微信公众号