检索模板目录的 URL 的更好方法是什么:bloginfo('template_url') 或 echo esc_url( get_template_directory_uri() )?

Zkk*_*Zkk 6 wordpress wordpress-theming

使用主题检查来测试我的主题的质量,它返回我的主题正在使用 Ex bloginfo();
<img src="<?php bloginfo('template_url'); ?>/static/img/logo.svg"

主题检查建议我替换bloginfo()forecho esc_url( get_template_directory_uri() );

我搜索了它,但我不确定使用这个函数是否是一个好的做法。

echo esc_url( get_template_directory_uri() );那么,调用我的主题中的任何文件是否正确?

And*_*sch 6

bloginfo('template_url')调用get_bloginfo('template_url', 'display')并且该函数检索 的输出get_template_directory_uri()

所以get_template_directory_uri()直接使用会减少2次函数调用

我不知道在这里使用是否esc_url()有意义。该函数get_template_directory_uri()有自己的一小部分来清理 URL:

$template = str_replace( '%2F', '/', rawurlencode( get_template() ) );
Run Code Online (Sandbox Code Playgroud)

来源:get_template_directory_uri()


在来自Automatic(wordpress背后的公司)的startertheme _s中,他们直接使用不带.get_template_directory_uri()esc_url()

看这里:functions.php


我的建议:

<img src="<?php echo get_template_directory_uri(); ?>/static/img/logo.svg"