织梦DedeCMS MIP文章内容页图片适配百度MIP规范教程

本教程介绍如何在不修改程序内核文件的前提下,通过增加自定义方法实现织梦MIP文章内容页图片适配百度MIP规范,不影响电脑站和手机站文章内容页的文章内容输出。

操作步骤

1. 将<img 替换成 <mip-img

2. 去除图片宽高(width|height)

3. 去除内联样式(style)

4. 将相对路径图片路径(/uploads/)替换成绝对路径

打开 /include/extend.func.php 添加自定义方法

在 /include/extend.func.php 文件最下面加入以下方法:

/**
 * MIP文章内容页图片适配百度MIP规范
 *
 * @access    public
 * @param     string  $content  文章内容
 * @return    string
 */
function mip($content){
    global $cfg_basehost;
    preg_match_all('/<img (.*?)\>/', $content, $images);
    if(!is_null($images)) {
        foreach($images[1] as $index => $value){
            $mip_img = str_replace('<img', '<mip-img', $images[0][$index]);
            $mip_img = str_replace('>', '></mip-img>', $mip_img);
            $mip_img = preg_replace('/(width|height)="\d*"\s/', '', $mip_img );
            $mip_img = preg_replace('/ style=\".*?\"/', '',$mip_img);
            $content = str_replace($images[0][$index], $mip_img, $content);
        }
    }
    preg_match_all('/ style=\".*?\"/', $content, $style);
    if(!is_null($style)) {
        foreach($style[0] as $index => $value){
            $mip_style = preg_replace('/ style=\".*?\"/', '',$style[0][$index]);
            $content = str_replace($style[0][$index], $mip_style, $content);
        }
    }
    $content = str_replace('/uploads/', $cfg_basehost.'/uploads/', $content);
    return $content;
}

内容页模板标签写法

在文章内容页模板中,使用以下标签调用自定义方法:

{dede:field.body function=mip(@me)/}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。