简体   繁体   中英

How to use relative urls when working with a wordpress site

关于如何设置wordpress以使用相对网址,是否有任何提示或建议?

I wrote a little PHP function that I located in the functions.php file of my theme:

function get_theme_relative_directory()
{
    return str_replace(get_site_url(), '', get_bloginfo('template_url'));
}

Using it in the theme files like this:

<link href="<?=get_theme_relative_directory()?>/style.css" type="text/css" media="all" rel="stylesheet">

Are you referring to when the editor, for example, inserts images?

If so, then I am not aware of any configuration setting to stop WordPress adding the full URL (eg. mysite.com/wp-content/uploads/image1.jpg) but you have the opportunity to edit it before clicking OK.

Yep, you can use my plugin: http://wordpress.org/extend/plugins/root-relative-urls/

It will convert all urls to root relative, including urls that are embedded via the editor. It will even convert rss feeds back to absolute (which is only necessary because companies like feedburner and feedblitz don't follow the html spec.)

The plugins that exist at the time of writing this lack complete coverage of the codebase. They only handle specific urls or specific scenarios. And you should never have to search & replace your database, Tim Berners Lee solved that problem way back in 1993.

Hope that helps and sorry I couldn't answer this question sooner, but I didn't write the plugin until a month ago or so :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM