将图像硬编码到父主题文件中的正确方式?

时间:2015-06-07 作者:Bjarni

我想问一下,使用bloginfo(\'template\\u directory\')将图像调用到父主题文件中是否是最佳做法?

<img src="<?php bloginfo(\'template_directory\'); ?>/img/logo.jpg" />
或者应该使用:

<img src="<?php echo get_template_directory(); ?>/img/logo.jpg" />

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

如果您查看法典,则使用get_bloginfo() 不鼓励使用某些参数:

返回主CSS (通常是style.css)活动主题的文件URL。考虑使用 get_stylesheet_uri() 取而代之的是

  • 样式表目录”-返回活动主题的样式表目录URL。(在早期的WordPress版本中是本地路径。)考虑使用 get_stylesheet_directory_uri() 取而代之的是
  • \'template\\u url\'/“template\\u directory”-活动主题目录的url(“template\\u directory”之前是本地路径2.6; 看见get_theme_root()get_template() 对于黑客的替代品。)在子主题中,get\\u bloginfo(\'template\\u url\')和get\\u template()都将返回父主题目录。考虑使用 get_template_directory_uri() 而是(对于父模板目录)或 get_stylesheet_directory_uri() (用于子模板目录)bloginfo(\'template_directory\') 不是最佳实践。请使用Codex中列出的函数之一-- get_template_directory_uri() get_stylesheet_directory_uri()

    我考虑使用bloginfo() 正如您在第一段代码中看到的,这是一种准弃用用法。有一些功能可以替代这种用法,但不管是不是这样,为什么要增加get_bloginfo() 什么时候您可以直接调用相应的函数,并且抄本本身告诉您这样做?

  • SO网友:Mathew Tinsley

    如果你查看来源get_bloginfo, 它所做的就是打电话get_template_directory_uri 当它通过参数时template_directorytemplate_url.

    请注意get_template_directory_uri 是用于获取URL的函数,而get_template_directory 是获取路径的函数。

    bloginfo(\'template_directory\')get_template_directory_uri 功能等效,但应用于bloginfo.

    使用两者都没有错。这两个函数都不能称为“硬编码”,因为它们都可以动态地将基本URL提取到当前主题(或者使用子主题时提取父主题)。

    结束