问题描述

我在阅读这篇文章:Common WordPress Development Mistakes and How to Fix Them,其中他们的作者说:

Getting the theme location: If you are using TEMPLATEPATH or bloginfo( ‘template_directory’). Stop! You should be using the very useful get_template_directory() as seen in my examples above.

但他没有详细说明。 bloginfo()有什么坏处?

最佳解决方案

要做一个很长的故事: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()

Edit

还要注意:TEMPLATEPATHSTYLESHEETPATH 都被淘汰,因此您现在可以分别使用 get_template_directory()get_stylesheet_directory()简单地替换它们。

编辑 2

Re:这个评论

Just what I was about to say :). I’d add that get_template_directory() returns the path and get_template_directory_uri() the url. So the author isn’t being consistent: get_bloginfo( 'template_directory' ) and get_template_directory() return different things!

参考来源:)

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

Re:这个评论:

The one caveat when developing child themes you’ll need to use get_stylesheet_directory() explicitly. Someone correct me if I’m wrong.

是的,但情况一直如此。 get_bloginfo()不返回与 get_stylesheet_directory()相关的任何内容。它只返回 get_stylesheet_uri()get_stylesheet_directory_uri()

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。