登录  /  注册
如何修改WordPress图片地址为相对路径
不言
发布: 2023-04-02 21:38:01
原创
2500人浏览过

这篇文章主要介绍了关于如何修改WordPress图片地址为相对路径,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

WordPress图片地址在默认编辑下是使用额绝对路径,这样别人复制你文章到其他的网站上,图片也可以正常显示,但是如果我想更改博客的域名,或者路径,那么这些图片的地址全部失效,不能正常显示。优搜网在网上找到两种解决Wordpress模板图片使用相对路径的方法,希望可以帮到大家。

1.修改Wordpress主题根目录下的wp-config.php,这个文件只有在安装好Wordpress之后才会出现,在该文件中加入一下两行

define(‘WP_HOME’, ”);
define(‘WP_SITEURL’, ”);
登录后复制

保存,OK了!但是这种修改方式是只能用户网网站根目录,并且使用默认的80端口

如果你不是用网站的根目录,或者用非80端口,那就用第二种方法

2.打开wp-includes/post.php文件,修改函数wp_get_attachment_url(3.7.1在4276行)为如下代码

function wp_get_attachment_url( $post_id = 0 ) {
$file_dir=dirname(__FILE__);
$server_root=$_SERVER[DOCUMENT_ROOT];
$file_dir=substr($file_dir,strlen($server_root));
$file_dir=substr($file_dir,0,-12);
if($file_dir!=”){
$file_dir=’/’.substr($file_dir,1);
}
$post_id = (int) $post_id;
if ( !$post =& get_post( $post_id ) )
return false;
$url = ”;
if ( $file = get_post_meta( $post->ID, ‘_wp_attached_file’, true) ) { //Get attached file
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directory
if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
//$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
$url=$file_dir.”/wp-content/uploads/”.$file;
elseif ( false !== strpos($file, ‘wp-content/uploads’) )
//$url = $uploads['baseurl'] . substr( $file, strpos($file, ‘wp-content/uploads’) + 18 );
$url=$file_dir.”/wp-content/uploads/”.$file;
else
//$url = $uploads['baseurl'] . “/$file”; //Its a newly uploaded file, therefor $file is relative to the basedir.
$url=$file_dir.”/wp-content/uploads/”.$file;
}
}
if ( empty($url) ) //If any of the above options failed, Fallback on the GUID as used pre-2.7, not recomended to rely upon this.
$url = get_the_guid( $post->ID );
if ( ‘attachment’ != $post->post_type || empty($url) )
return false;
return apply_filters( ‘wp_get_attachment_url’, $url, $post->ID );
}
登录后复制

保存,OK了

这样你以后写的文章日志里面的多媒体文件路径都是用相对路径了,更换域名空间之后图片地址不会失效!

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

WordPress支持多个域名绑定/访问的实现

以上就是如何修改WordPress图片地址为相对路径的详细内容,更多请关注php中文网其它相关文章!

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学