home_url()和site_url()有什么区别
-
-
您在一个非常重要的问题上一次要问两个问题.答案为" home_url()和site_url()有什么区别?"与"我如何让WordPress返回没有安装子目录的URL根目录?"这一问题不同.You're asking two questions at once on a very important question. The answer to "What's the difference between home_url() and site_url()?" is different than the question, "How do I get WordPress to return the URL root without the subdirectory where it's installed?"
- 4
- 2012-04-29
- Volomike
-
查看这些法典指南:https://codex.wordpress.org/Editing_wp-config.php#WordPress_address_.28URL.29;https://codex.wordpress.org/Editing_wp-config.php#Blog_address_.28URL.29;http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Using_a_pre-existing_subdirectory_installReview these codex guides: https://codex.wordpress.org/Editing_wp-config.php#WordPress_address_.28URL.29 ; https://codex.wordpress.org/Editing_wp-config.php#Blog_address_.28URL.29 ; http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory#Using_a_pre-existing_subdirectory_install
- 0
- 2016-10-07
- Tara
-
5 个回答
- 投票数
-
- 2012-04-30
您一次要问两个问题:
-
home_url()
和site_url()
之间有什么区别? - 如何让WordPress返回没有安装子目录的URL根目录?
这是答案,我向WordPress的核心开发人员安德鲁·纳辛(Andrew Nacin)确认,并进行了一些服务器测试以确认安德鲁告诉我的内容.
问题1
在wp-admin的"常规">"设置"中,
home_url()
引用标有"站点地址(URL)"的字段. 令人困惑,是吗?是的,它说的是"站点地址",因此您可能会假设site_url()
,但是您会错了.运行您自己的测试,您会看到. (您可以在主题的functions.php顶部使用site_url()
和home_url()
值临时放置一个home_url()
字段. )与此同时,
site_url()
引用"常规">"设置"中标记为" WordPress地址(URL)"的字段.因此,如果要引用物理路径,例如在URL上调用插件的文件夹路径以加载图像,或调用主题的文件夹路径以加载图像,则实际上应该使用其他功能对于这些-查看
plugins_url()
和get_template_directory_uri()
.site_url()
将始终是您可以通过以下方式添加到站点的位置:最后通过粘贴/wp-admin
,而home_url()
不能可靠地定位在这个位置.home_url()
将是通过设置常规>设置"站点地址(URL)"字段来设置主页的位置.问题2
因此,如果我将博客放在
http://example.com/blog
中,并且example.com
只是一些静态网站,组合主题,那么这将是一个符合您问题的方案.在这种情况下,我将使用以下代码段:<?php function getDomain() { $sURL = site_url(); // WordPress function $asParts = parse_url( $sURL ); // PHP function if ( ! $asParts ) wp_die( 'ERROR: Path corrupt for parsing.' ); // replace this with a better error result $sScheme = $asParts['scheme']; $nPort = $asParts['port']; $sHost = $asParts['host']; $nPort = 80 == $nPort ? '' : $nPort; $nPort = 'https' == $sScheme AND 443 == $nPort ? '' : $nPort; $sPort = ! empty( $sPort ) ? ":$nPort" : ''; $sReturn = $sScheme . '://' . $sHost . $sPort; return $sReturn; }
You are asking two questions at once:
- What's the difference between
home_url()
andsite_url()
? - How do I get WordPress to return the URL root without the subdirectory where it's installed?
Here are the answers, and I confirmed with Andrew Nacin, a core developer of WordPress, as well as ran some server tests to confirm what Andrew told me.
Question # 1
In General > Settings of wp-admin,
home_url()
references the field labeled "Site Address (URL)". Confusing, huh? Yeah, it says "Site Address" so you might assumesite_url()
, but you'd be wrong. Run your own test and you'll see. (You can temporarily drop anecho H1
field withsite_url()
andhome_url()
values at the top of your your theme's functions.php.)Meanwhile,
site_url()
references the field labeled "WordPress Address (URL)" in General > Settings.So, if you're wanting to reference where a physical path might be such as calling a plugin's folder path on the URL to load an image, or calling a theme's folder path to load an image, you should actually use other functions for those - look at
plugins_url()
andget_template_directory_uri()
.The
site_url()
will always be the location where you can reach the site by tacking on/wp-admin
on the end, whilehome_url()
would not reliably be this location.The
home_url()
would be where you have set your homepage by setting General > Settings "Site Address (URL)" field.Question # 2
So, if I have placed my blog in
http://example.com/blog
, andexample.com
is just some static site where I have like a portfolio theme, then this would be a scenario that lines up with your question. In such a case, then I would use this snippet of code:<?php function getDomain() { $sURL = site_url(); // WordPress function $asParts = parse_url( $sURL ); // PHP function if ( ! $asParts ) wp_die( 'ERROR: Path corrupt for parsing.' ); // replace this with a better error result $sScheme = $asParts['scheme']; $nPort = $asParts['port']; $sHost = $asParts['host']; $nPort = 80 == $nPort ? '' : $nPort; $nPort = 'https' == $sScheme AND 443 == $nPort ? '' : $nPort; $sPort = ! empty( $sPort ) ? ":$nPort" : ''; $sReturn = $sScheme . '://' . $sHost . $sPort; return $sReturn; }
-
您是否有与A.Nacin进行讨论的链接?Do you have a link to the discussion with A.Nacin?
- 0
- 2012-04-30
- kaiser
-
是通过电子邮件.抱歉.哦,感谢您的修改-我会记住下一次语法更改.It was via email. Sorry. Oh, and thanks for the edit -- I'll remember that syntax change next time.
- 1
- 2012-04-30
- Volomike
-
我花了很长时间并且很痛苦地意识到'Site Address(URL)'='home'和'WordPress Address(URL)'='siteurl'.他们绝对应该更改这些标签.It took me a very long time and a lot of pain to realize that 'Site Address (URL)' = 'home' and 'WordPress Address (URL)' = 'siteurl'. They should definitely change those labels.
- 8
- 2014-01-26
- Jbm
-
您对第二个问题的回答中奖了!Your answer to the second question hits the jackpot!
- 0
- 2018-02-19
- Devner
-
- 2011-06-17
如果要将WP安装在目录中,但将站点安装在域根目录下,则需要将index.php主文件移至域根目录,然后编辑require语句以指向目录.
此处概述了此过程:为WordPress提供自己的目录.
If you want WP installed in a directory but the site home on your domain root, you need to move the main index.php file out to your domain root and edit the require statement to point within your directory.
This process is outlined here: Giving WordPress Its Own Directory.
-
由于我处于wp网络模式,因此我总是只使用`home_url()`.我只给过WordPress一个自己的目录,但这并不是我的喜好.但是我确实在某些站点上使用了" wp_content_dir".I always just use `home_url()` since i am on wp network mode. I have only given WordPress its own directory once and it just was not my liking. But i do however use the `wp_content_dir` on some sites.
- 0
- 2011-06-17
- xLRDxREVENGEx
-
我没有使用多站点的经验,所以我不熟悉这种情况下的工作原理.我更喜欢将WP安装在目录中,只是为了保持环境整洁而不使根混乱.I don't have any experience with multisite, so I'm not familiar how this stuff works in that situation. I prefer installing WP in a directory just to keep things clean and not clutter the root.
- 0
- 2011-06-17
- Milo
-
我的文件结构可能是最整洁的文件结构之一.`home/usr/public_html/site1``home/usr/public_html/site2`等等,然后`wp_content_dir`通常在CDN上my file structure is probably one of the neatest ones around. `home/usr/public_html/site1` `home/usr/public_html/site2` and so on and then the `wp_content_dir` is usually on a cdn
- 0
- 2011-06-17
- xLRDxREVENGEx
-
如果仅安装WP会很好,但是我主要是在其他人的服务器上工作,其中有成百上千个文件和目录.if a WP install were the only thing there it would be fine, but I'm mostly working on other peoples servers with hundreds of files and directories littered about.
- 0
- 2011-06-17
- Milo
-
我的理解正确吗,除非site_url()和home_url()相同,除非将其wordpress安装目录设置为不同于根目录?Is my understanding correct that site_url() and home_url() are the same, unless one sets up their wordpress install directory to be different than the root?
- 0
- 2011-06-20
- Praveen
-
- 2013-04-17
site_url()
和home_url()
函数相似,可能会导致工作方式混乱.site_url()
函数在数据库的siteurl
表中检索wp_options
的值.这是WordPress核心文件的URL.
如果您的核心文件位于Web服务器上的子目录/wordpress
中,则该值为http://example.com/wordpress
.home_url()
函数在数据库的siteurl
表中检索wp_options
的值.这是您希望人们访问以查看您的WordPress网站的地址.
如果您的WordPress核心文件位于
/wordpress
中,但是您希望网站URL为http://example.com
,则起始值应为http://example.com
.The
site_url()
andhome_url()
functions are similar and can lead to confusion in how they work.The
site_url()
function retrieves the value value forsiteurl
in thewp_options
table in your database.This is the URL to the WordPress core files.
If your core files exist in a subdirectory/wordpress
on your web server, the value would behttp://example.com/wordpress
.The
home_url()
function retrieves the value forhome
in thewp_options
table in your database.This is the address you want people to visit to view your WordPress web site.
If your WordPress core files exist in
/wordpress
, but you want your web site URL to behttp://example.com
the home value should behttp://example.com
. -
- 2011-06-17
要回答第二个问题:
问:如果正确,那么我可以让wordpress返回 http://example.com/吗?
您不能这样做,除非您采取为WordPress提供其自己的目录步骤.使用此方法意味着您将WordPress核心文件放入
/blog
或/WordPress
,然后将index.php
放入您的根目录.如果您决定将WordPress放在其自己的目录中,则可以使用
home_url()
进入index.php
和site_url()
获取核心文件等.推荐人:
site_url
的法典
home_url
的法典
提供Wordpress自己目录的法典To answer your second question:
Q: If that's correct, then can I get wordpress to return http://example.com/ ?
You can't, unless you take the Giving WordPress its own directory steps. Using this means you put WordPress core files into
/blog
or/WordPress
and then theindex.php
into your root.If you decide to put WordPress inside its own directory then you would use
home_url()
for going toindex.php
andsite_url()
for getting core files and such.Refrences:
Codex forsite_url
Codex forhome_url
Codex for Giving Wordpress Own Directory -
- 2016-05-05
获取没有任何子目录的网站网址的最简单方法(改为 http://example.com/( http://example.com/blog )中,只需使用反斜杠/
例如,如果您输入:
<a href="/">domain url</a>
它将创建一个链接到您的域
The easiest way to get the site url without any sub-directories ( http://example.com/ instead of http://example.com/blog ), just use the backslash /
For example, if you type:
<a href="/">domain url</a>
It will create a link that goes to your domain
-
感谢您的参与.不幸的是,这不能回答OP提出的问题.人们需要使用OP询问的wordpress功能有很多原因.OP不太可能只想通过html(例如通过编辑帖子)将链接添加到其主页.OP更有可能编辑php主题文件或插件文件.无论如何,他们使用的是php,而不是html.最后,虽然OP为这个站点的期望值没有`/`值,但在另一个站点上,OP可能会期望返回一个子目录.这取决于每个站点的WP配置.Thanks for participating. Unfortunately, this does not answer the question posed by the OP. There are many reasons why a person needs to use the wordpress functions OP asks about. It is unlikely that OP simply wants to add link to their home page via html, such as by editing a post. It is more likely, OP is editing a php theme file, or a plugin file. In any case, they are working with php, not html. Finally, while OP *expected* a value without `/` for *this* site, on a different site, OP may *expect* a subdirectory to be returned. It depends on the WP configuration for each site.
- 0
- 2019-01-05
- SherylHohman
我的理解是
site_url()
返回WordPress核心文件所在的位置.如果我的博客托管在
http://example.com/blog
上,则site_url()
返回http://example.com/blog
但是
home_url()
有何不同?对我来说,home_url()
返回相同的内容:http://example.com/blog
如果正确,那么我可以让WordPress返回
http://example.com/
吗?