如何获取PHP中的主题URL?
4 个回答
- 投票数
-
- 2010-08-21
此功能将返回主题目录URL,以便您可以在其他功能中使用它:
get_bloginfo('template_directory');
或者,此功能将回显主题目录URL到浏览器:
bloginfo('template_directory');
因此,主题
images/headers
文件夹中的图像的示例为:<img src="<?php bloginfo('template_directory'); ?>/images/headers/image.jpg" />
This function will return the theme directory URL so you can use it in other functions:
get_bloginfo('template_directory');
Alternatively, this function will echo the theme directory URL to the browser:
bloginfo('template_directory');
So an example for an image in the themes
images/headers
folder would be:<img src="<?php bloginfo('template_directory'); ?>/images/headers/image.jpg" />
-
注意:如果您当前正在使用子主题,而不是活动的子主题,这将为您提供*parent *主题的路径.以下较长的答案将对此进行更详细的说明.NOTE: this will give you the path to the *parent* theme if you are currently using a child theme, and not the active child theme. A longer answer below explains this in more detail.
- 0
- 2016-10-19
- Jason
-
您可以简单地使用`get_template_directory_uri()`You can simply use `get_template_directory_uri()`
- 2
- 2018-06-20
- Pei
-
- 2010-08-21
@EAMann 所说的,但有警告.埃里克(Eric)关于通用方法以及
bloginfo()
和get_bloginfo()
函数如何工作以及如何传递参数'template_directory'
是正确的以获得您(大多数)主题所需的价值.但是需要注意的是, caveat 与更新的 Child Themes 有关.如果您使用的是子主题,那么
'template_directory'
可能不是您想要的,除非您实际上尝试引用父主题目录中的图像.取而代之的是,对于子主题,您可能想要通过stylesheet_directory
(我知道,名字不告诉您它们是什么,但是,嘿,这就是这样!) Eric使用stylesheet_directory
的回复看起来像这样(我简化了示例,所以它不会包装):&lt;img src="&lt;?phpbloginfo('stylesheet_directory');?&gt;/images/header.jpg"/&gt;
为了说明我编写的一个快速独立文件的观点,您可以将其根目录放置为
test.php
并运行以查看其输出.首先以TwentyTen这样的常规主题运行,然后以子主题运行:&lt;?php /* *test.php-测试常规主题和子主题之间的区别 * */ 包括" wp-load.php"; $bloginfo_params=数组( 'admin_email', 'atom_url', "字符集", 'comments_atom_url', 'comments_rss2_url', '描述', '家', 'html_type', '语言', '名称', 'pingback_url', 'rdf_url', 'rss2_url', 'rss_url', " siteurl", 'stylesheet_directory', 'stylesheet_url', 'template_directory', 'template_url', 'text_direction', 'url', '版', " wpurl", ); 回声'&lt;tableborder=" 1"&gt;'; foreach($bloginfo_params as $param){ $info=get_bloginfo($param); echo"&lt;tr&lt;th&gt; {$param}:&lt;/th&lt;td&gt; {$info}&lt;/td&lt;/tr&gt;"; } 回声'&lt;/table&gt;';
如果您发现了什么,您可能会注意到传递给
bloginfo()
和get_bloginfo()
的内容还有很多.请研究下面的代码和屏幕截图以获取想法.看截图,您可以看到对于常规主题,
stylesheet_directory
返回的内容与'template_directory'
相同,但是值有所不同,可能还需要一个儿童主题.
(来源:mikeschinkel.com ) >为清晰起见,
wp30.dev
是仅运行的域 在我的本地计算机上.目前是 WordPress 3.0.1的实例及其 配置为127.0.0.1
(与localhost
),然后使用它 用于测试类似的临时示例. 我在Mac OS X上使用 VirtualHostX 作为便利来帮助我设置那些私有的不可路由的.dev
域,但任何人都可以通过以下方式手动完成 编辑计算机的主机文件和 ? httpd.conf文件.如果您不熟悉 儿童主题 ,顺便说一下,还有另外两个可能有帮助的WordPress答案:
What @EAMann said, with a caveat. Eric is right about the general approach and how the functions
bloginfo()
andget_bloginfo()
work and about how to pass the parameter'template_directory'
to get the value you need for (most) themes.However there is a caveat and that caveat is with the newer Child Themes. If you are using a child theme then
'template_directory'
is probably not what you want unless you are actually trying to refer to an image that is in the parent theme directory. Instead for child themes what you probably want is to passstylesheet_directory
(I know, I know, the names don't tell you what they are but hey, that's just the way it is!) Borrowing somewhat from Eric's reply usingstylesheet_directory
would look like this (I shortened the example so it would not wrap):<img src="<?php bloginfo('stylesheet_directory'); ?>/images/header.jpg" />
To illustrate the point I wrote a quick standalone file you can drop in your website's root as
test.php
and run to see what it outputs. First run with a regular theme like TwentyTen then run with a child theme:<?php /* * test.php - Test the difference between Regular and Child Themes * */ include "wp-load.php"; $bloginfo_params = array( 'admin_email', 'atom_url', 'charset', 'comments_atom_url', 'comments_rss2_url', 'description', 'home', 'html_type', 'language', 'name', 'pingback_url', 'rdf_url', 'rss2_url', 'rss_url', 'siteurl', 'stylesheet_directory', 'stylesheet_url', 'template_directory', 'template_url', 'text_direction', 'url', 'version', 'wpurl', ); echo '<table border="1">'; foreach($bloginfo_params as $param) { $info = get_bloginfo($param); echo "<tr><th>{$param}:</th><td>{$info}</td></tr>"; } echo '</table>';
If you notice things you might notice that there's a lot more to what you can pass to
bloginfo()
andget_bloginfo()
; study the code and the screenshot below for ideas.Looking at the screenshot you can see that
stylesheet_directory
returns the same thing as'template_directory'
for a regular theme but a different value, and probably the value you need for a child theme.
(source: mikeschinkel.com)For clarity on this screenshot,
wp30.dev
is a domain that runs only on my local computer. It is currently an instance of WordPress 3.0.1 and it is configured at127.0.0.1
(same aslocalhost
) on my laptop and I use it for testing ad-hoc examples like this. I used VirtualHostX as a convenience on the Mac OS X to help me set up those private non-routable.dev
domains but anyone can do it manually by editing the computer's hosts file and the ? httpd.conf file.By the way, in case you are not familiar with Child Themes where are two other WordPress Answers that might help:
-
哇,好答案.我对现在正在研究的主题感到很懒,并且没有设置儿童主题,但这在将来会很有帮助.也祝贺该脚本.编码正确.谢谢!Wow, great answer. I was lazy with the theme I'm working on now and didn't set up a child theme, but this will be very helpful in the future. Congratulations on that script too. Well-coded. Thanks!
- 0
- 2010-08-21
- Michael Crenshaw
-
嗨,不错的答案!我通常使用`get_stylesheet_directory_uri()`.我应该使用普通的'get_stylesheet_directory()`吗?Hi, nice answer! I usually use `get_stylesheet_directory_uri()`. Should I be using plain ol' `get_stylesheet_directory()`?
- 0
- 2012-01-18
- djb
-
- 2012-03-26
主题的整个结构基于两个选项-
template
(保存父主题文件夹名称)和stylesheet
(保存子主题文件夹名称).如果没有使用子主题,则它们是相同的.要具有过滤器的灵活性,而不是直接访问选项,因此有
get_template()
和get_stylesheet()
.现在唯一缺少的是将那些与主题文件夹位置组合在一起.可以使用
get_theme_root_uri()
来完成,并再次方便地包装在get_template_directory_uri()
和 < code>get_stylesheet_directory_uri() .[get_]bloginfo()
和template_directory
或stylesheet_directory
参数只是将它们包装起来,因此没有理由使用它.我要说的是,只有通过说目录(通常与本地路径有关)而返回URL来混淆.摘要:
- 使用
get_template_directory_uri()
引用仅父主题 - 使用
get_stylesheet_directory_uri()
来仅儿童还是儿童主题
The whole structure of theme builds on top of two options -
template
(holding parent theme folder namre) andstylesheet
(holding child theme folder namr). If there is no child theme used these are the same.To have flexibility of filters, rather than access option directly, there are accordingly
get_template()
andget_stylesheet()
.Now the only thing is missing is to combine those with themes folder location. This can be done with
get_theme_root_uri()
and again conveniently wrapped inget_template_directory_uri()
andget_stylesheet_directory_uri()
.[get_]bloginfo()
withtemplate_directory
orstylesheet_directory
arguments merely wraps these and there is little reason to use it like that. I'd say it is only confusing by having argument saying directory (commonly relates to local paths), but returning URLs.Sumary:
- use
get_template_directory_uri()
to refer to only or parent theme - use
get_stylesheet_directory_uri()
to only or child theme
-
-
没有解释为什么这比其他解决方案更好?No explanation why this is better than the other solutions?
- 1
- 2012-11-05
- fuxia
-
我需要获取主题目录的URL才能引用主题的image/headers目录中的图像.用PHP如何完成?