How to PHP include a theme file
-
I am having trouble with what I assumed was an everyday PHP use case.
I have some SVG files that I want to insert into the index.php template. The files are saved in one of the theme directories.
I assumed this would be a simple include statement:
<?php include('/images/icon.svg'); ?>
This worked great in my local development environment, but the file did not display when I put it on a live server for testing. So, I thought I would try to be more precise and use
get_template_directory_uri();
, but I could not figure out how to make it work as part of the include statement. I also triedbloginfo('template_url');
, but I could not get it to work inside the include statement either.So, I decided to just hard code the full path.
<?php include('/wp-content/themes/theme-name/images/icon.svg'); ?>
Once again, this worked locally but now it does not display on the testing server. The file definitely exists; if I type the full URL into the browser it loads the file. I can also insert the file using an
<img>
tag without any problems – but that will not work for my use case.Can anyone provide advice for what I am getting wrong?
- The topic ‘How to PHP include a theme file’ is closed to new replies.