具体干货如下,将其放入主题的 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');
友情提示:注意看代码里的注释哦!
做猪小侠源码的代理,提供一站式服务
如果你不懂得搭建网站或者服务器,小程序,源码之类的怎么办? 第一通过本站学习各种互联网的技术 第二就是联系客服,我帮帮你搭建(当然要收取部分的费用) 第三成为我们的代理,我们提供整套的服务。