如何修改WordPress图片地址为相对路径
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何修改WordPress图片地址为相对路径,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2026字,纯文字阅读大概需要3分钟。
内容图文
https://blog.csdn.net/lizhi125/article/details/16940617
WordPress图片地址在默认编辑下是使用额绝对路径,这样别人复制你文章到其他的网站上,图片也可以正常显示,但是如果我想更改博客的域名,或者路径,那么这些图片的地址全部失效,不能正常显示。优搜网在网上找到两种解决Wordpress模板图片使用相对路径的方法,希望可以帮到大家。
1.修改Wordpress主题根目录下的wp-config.php,这个文件只有在安装好Wordpress之后才会出现,在该文件中加入一下两行
如果你不是用网站的根目录,或者用非80端口,那就用第二种方法
2.打开wp-includes/post.php文件,修改函数wp_get_attachment_url(3.7.1在4276行)为如下代码
function wp_get_attachment_url( $post_id = 0 ) {
$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了
这样你以后写的文章日志里面的多媒体文件路径都是用相对路径了,更换域名空间之后图片地址不会失效!
原文:https://www.cnblogs.com/beida/p/9279964.html
内容总结
以上是互联网集市为您收集整理的如何修改WordPress图片地址为相对路径全部内容,希望文章能够帮你解决如何修改WordPress图片地址为相对路径所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。