php 我可以在模板文件中的 WordPress 中获得“基本 URL”吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get a "base URL" in WordPress within a template file?
提问by alex
Usually in my PHP apps I have a base URLsetup so I can do things like this
通常在我的 PHP 应用程序中,我有一个基本 URL设置,所以我可以做这样的事情
<a href="<?php echo BASE_URL; ?>tom/jones">Tom</a>
Then I can move my site from development to production and swap it easily and have the change go site wide (and it seems more reliable than <base href="" />.
然后我可以将我的网站从开发转移到生产并轻松交换它并使更改在整个站点范围内进行(并且它似乎比<base href="" />.
I'm doing up a WordPress theme, and I am wondering, does WordPress have anything like this built in, or do I need to redefine my own?
我正在做一个 WordPress 主题,我想知道,WordPress 是否内置了这样的东西,或者我需要重新定义我自己的吗?
I can see ABSPATH, but that is the absolute file path in the file system, not something from the document root.
我可以看到ABSPATH,但这是文件系统中的绝对文件路径,而不是文档根目录中的内容。
回答by hsatterwhite
get_bloginfo('wpurl');would be the preferred method of getting the base url of your WordPress installation. This always returns the absolute base url for the install where as get_bloginfo('url');is for the actual blog address of your WordPress install.
get_bloginfo('wpurl');将是获取 WordPress 安装的基本 url 的首选方法。这总是返回安装的绝对基本 url,其中get_bloginfo('url');WordPress 安装的实际博客地址也是如此。
回答by alex
Yes, you can use get_bloginfo('url')just like that or define a constant...
是的,您可以get_bloginfo('url')像这样使用或定义一个常量......
define('BASE_URL', get_bloginfo('url'));
If you are working on a template and want the URL fragment to that theme folder, use...
如果您正在处理模板并希望 URL 片段指向该主题文件夹,请使用...
bloginfo('template_directory');
回答by Shwet
Yes you can get "base URL" with a simple function.
是的,您可以通过一个简单的函数获得“基本 URL”。
<?php echo get_bloginfo('url') ?>
after that with /you can reach to any page just type the page name.
之后,/您可以访问任何页面,只需输入页面名称即可。
回答by Dipak Mahajan
You can try using
您可以尝试使用
<?php echo home_url(); ?>
By using this can get site url like www.xyz.com
通过使用它可以获得像 www.xyz.com 这样的站点 url
<?php echo home_url('/contact'); ?>
By using this syntax you will get url like www.xyz.com/contact
通过使用此语法,您将获得类似 www.xyz.com/contact 的网址

