get_template_directory()vs bloginfo('template_directory')vs TEMPLATEPATH


Answers:


17

为了使长话短说:get_bloginfo( 'template_directory' )get_bloginfo( 'template_url' )简单的返回get_template_directory_uri()

因此,您可以简单地通过直接引用后者的模板标签来简化第二个调用。

请参阅来源get_bloginfo()

其他一些:

  • 'url' => home_url()
  • 'wpurl' => site_url()
  • 'stylesheet_url' => get_stylesheet_uri()
  • 'stylesheet_directory' => get_stylesheet_directory_uri()
  • 'locale' => get_locale()

编辑

另请注意:TEMPLATEPATHSTYLESHEETPATH预定要弃用,因此最好现在分别用get_template_directory()和替换它们get_stylesheet_directory()

编辑2

回复:此评论

我刚才要说的是:)。我将添加get_template_directory()返回路径和get_template_directory_uri()URL。因此作者并不一致:get_bloginfo( 'template_directory' )get_template_directory()返回不同的东西!

请参考源代码:)

case 'template_directory':
case 'template_url':
    $output = get_template_directory_uri();
    break;

回复:此评论:

在开发子主题时,您需要get_stylesheet_directory()明确使用一个警告。如果我错了,有人纠正我。

没错,但情况一直如此。get_bloginfo()不返回与相关的任何信息get_stylesheet_directory()。它仅返回get_stylesheet_uri()get_stylesheet_directory_uri()


开发子主题时需要注意的一件事是,您需要显式使用get_stylesheet_directory()。如果我错了,有人纠正我。
史蒂夫

谢谢!我只是想知道为什么WP具有如此众多的功能而只是调用另一个功能,以至于我们不应该使用它们……
coopersita 2012年

在这种情况下,我认为原因仅仅是为了保持对get_bloginfo()参数的向后兼容性。
Chip Bennett 2012年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.