質問

I wanted to ask if ths is the best practice for calling an image into a parent theme file by using bloginfo('template_directory')?

<img src="<?php bloginfo('template_directory'); ?>/img/logo.jpg" />

or should one use:

<img src="<?php echo get_template_directory(); ?>/img/logo.jpg" />
役に立ちましたか?

解決

If you check the Codex, the use of get_bloginfo() for certain parameters is discouraged:

  • 'stylesheet_url' - Returns the primary CSS (usually style.css) file URL of the active theme. Consider using get_stylesheet_uri() instead.
  • 'stylesheet_directory' - Returns the stylesheet directory URL of the active theme. (Was a local path in earlier WordPress versions.) Consider using get_stylesheet_directory_uri() instead.
  • 'template_url' / 'template_directory' - URL of the active theme's directory ('template_directory' was a local path before 2.6; see get_theme_root() and get_template() for hackish alternatives.) Within child themes, both get_bloginfo('template_url') and get_template() will return the parent theme directory. Consider using get_template_directory_uri() instead (for the parent template directory) or get_stylesheet_directory_uri() (for the child template directory).

So, the answer to your question is that bloginfo('template_directory') is not best practice. Use one the functions listed in Codex instead-- get_template_directory_uri() or get_stylesheet_directory_uri()

I consider using bloginfo() as you have in the first block of code to be a kind of quasi-deprecated usage. There are functions in place to replace that usage but that being the case or not why add the overhead of get_bloginfo() when you can call the appropriate functions directly, and are told to do so by the Codex itself?

他のヒント

If you look at the source for get_bloginfo, all it is doing is calling get_template_directory_uri when it is passed the argument template_directory or template_url.

Note that get_template_directory_uri is the function for getting the URL whereas get_template_directory is the function for getting the path.

bloginfo('template_directory') and get_template_directory_uri are functionally equivalent, with the possible exception of filters applied to bloginfo.

There is nothing wrong with using either. Neither function could be called "hard coded" as they both dynamically fetch the base URL to the current theme (or parent theme when using a child theme).

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top