具体干货如下,将其放入主题的 functions.php 中即可~

/**
 * WordPress如何实现文章图片自动替换为指定图片
 * https://www.freexyz.cn/
 * date:2018-3-12 11:09:24
 */
function lxtx_replace_postimages( $content ){
    $reg = array('#(http://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#','#(https://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#');
    $pic = get_template_directory_uri().'/images/default.jpg'; // 使用主题根目录的images文件夹中的default.jpg做为默认输出图片
    // 如若想输出随机图片,则取消下面2行注释,并在主题根目录的images文件夹中创建一个random文件夹并放置10张随机图片,图片命名为:1.jpg  2.jpg ...  10.jpg
    // $random = mt_rand(1, 10);
    // $pic = get_template_directory_uri().'/images/random/'.$random.'.jpg';
    $content = preg_replace($reg, $pic, $content);
    return $content;
}
add_filter('the_content', 'lxtx_replace_postimages');

友情提示:注意看代码里的注释哦!


WordPress

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