wordpress搭建的站点无论是更换域名还是搭建测试系统,在正式系统是真正运行后都会存在绝对路径的问题,但这两项又是无法避免的,具体有如下方法可以执行:
修改wp-config.php文件
在require_once(ABSPATH . 'wp-settings.php');上面添加
<?php $home = 'http://'.$_SERVER['HTTP_HOST']; $siteurl = 'http://'.$_SERVER['HTTP_HOST']; define('WP_HOME', $home); define('WP_SITEURL', $siteurl); ?>
这样修改后,在后台的 setting-options 页面,只能显示而不能修改home和siteurl两个值(因为它们已经被定义成两个常量了,呵呵),它们完全是根据访问域名显示的。
修改多媒体插件
更改多媒体插件插入图片时使用相对路径!
另外两种方式如下:
1.修改wordpress根目录下的wp-config.php,这个文件只有在安装好wordpress之后才会出现,在该文件中加入一下两行
define('WP_HOME', ""); define('WP_SITEURL', "");
保存,OK了!但是这种修改方式是只能用户网网站根目录,并且使用默认的80端口。
如果你不是用网站的根目录,或者用非80端口,那就用第二种方法:
2.打开wp-includes/post.php文件,修改函数wp_get_attachment_url为如下代码:
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 ); }
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏