我需要获取主题目录的URL,以引用主题的图像/标题目录中的图像。这在PHP中是如何实现的?
如何在PHP中获取主题URL?
此函数将返回主题目录URL,以便您可以在其他函数中使用它:
get_bloginfo(\'template_directory\');
或者,此功能将主题目录URL回送到浏览器:bloginfo(\'template_directory\');
主题中的图像示例images/headers
文件夹应为:<img src="<?php bloginfo(\'template_directory\'); ?>/images/headers/image.jpg" />
埃曼说了些什么,并提出了警告。Eric对一般方法和功能的理解是正确的bloginfo()
和get_bloginfo()
工作和关于如何传递参数\'template_directory\'
获得(大多数)主题所需的价值。
然而,有一个警告caveat 较新的是Child Themes. 如果你使用的是儿童主题,那么\'template_directory\'
可能不是您想要的,除非您实际尝试引用父主题目录中的图像。相反,对于子主题,您可能希望传递stylesheet_directory
(我知道,我知道,名字不能告诉你它们是什么,但嘿,就是这样!)借用Eric的回答,使用stylesheet_directory
如下所示(我缩短了示例,使其不会换行):
<img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/header.jpg" />
test.php
然后运行以查看它的输出。首先使用常规主题(如TwentyTen)跑步,然后使用子主题跑步:<?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>\';
如果你注意到了一些事情,你可能会注意到,你可以传递的信息还有很多bloginfo()
和get_bloginfo()
; 研究下面的代码和屏幕截图以获取想法。看看屏幕截图,你可以看到stylesheet_directory
返回与相同的内容\'template_directory\'
对于常规主题,但值不同,并且可能是子主题所需的值。
(来源:mikeschinkel.com)
为清晰显示此屏幕截图,wp30.dev
是一个仅在本地计算机上运行的域。它当前是WordPress 3.0.1的一个实例,配置在127.0.0.1
(与相同localhost
) 在我的笔记本电脑上,我用它来测试像这样的特殊例子。我曾经VirtualHostX 为了方便在Mac OS X上设置那些私有的不可路由.dev
但任何人都可以通过编辑计算机的主机文件和?httpd。conf文件。
顺便说一下,如果你不熟悉Child Themes WordPress的另外两个答案可能会有所帮助:
主题的整个结构建立在两个选项之上—template
(保存父主题文件夹namre)和stylesheet
(保存子主题文件夹namr)。如果没有使用子主题,则它们是相同的。
为了使过滤器具有灵活性,而不是直接访问选项,有相应的get_template()
和get_stylesheet()
.
现在唯一缺少的是将它们与主题文件夹位置相结合。这可以通过get_theme_root_uri()
又一次方便地裹上get_template_directory_uri()
和get_stylesheet_directory_uri()
.
[get_]bloginfo()
具有template_directory
或stylesheet_directory
争论仅仅是对这些进行包装,没有什么理由这样使用它。我想说的是,只有当参数说目录(通常与本地路径相关)但返回URL时,才令人困惑。
Sumary公司:
使用get_template_directory_uri()
参考only or parent 主题使用get_stylesheet_directory_uri()
到only or child 主题
I use this(dirname(get_bloginfo(\'stylesheet_url\')))