如何修改WordPress圖片地址為相對路徑

來源:互聯網
上載者:User
這篇文章主要介紹了關於如何修改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 fileif ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { //Get upload directoryif ( 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了

這樣你以後寫的文章日誌裡面的多媒體檔案路徑都是用相對路徑了,更換網域名稱空間之後圖片地址不會失效!

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.