wordpress get_template_directory

2020-07-27 02:43发布

I have the following code, what I am expecting it to do is locate the theme I am using and place that path before the /images/bg.png so far I just get the location which is localhost/nameoffolder/images/bg.png but what i want is localhost/nameoffolder/wp-content/themes/images/bg.png

<img src="<?php get_template_directory(); ?>/images/bg.png"></img>

this is the directory I would like to display http://localhost:8888/fiftyfity/wp-content/themes/fiftyfity/images/

3条回答
时光不老,我们不散
2楼-- · 2020-07-27 02:58

that's odd... Can you try get_stylesheet_directory() instead and see what that returns?

查看更多
可以哭但决不认输i
3楼-- · 2020-07-27 02:59

I used this code instead and it worked

<img src="<?php bloginfo( 'template_directory' ); ?>/images/bg.png"></img>
查看更多
放荡不羁爱自由
4楼-- · 2020-07-27 03:00

get_template_directory() returns the directory, but it doesn't echo it.. so if you use it with echo it will work

<img src="<?php echo get_template_directory(); ?>/images/bg.png"></img>

but I will suggest to use get_template_directory_uri() instead, because above will not be portable ( windows use backward slashes as separator )

<img src="<?php echo get_template_directory_uri(); ?>/images/bg.png"></img>
查看更多
登录 后发表回答