get_template_directory / get_template_directory_uri not working in WP?

This article was written by in May 9, 2014, & may not be posted on other sites!
Original source url for this article: get_template_directory / get_template_directory_uri not working in WP?

Trying to use get_template_directory(); or get_template_directory_uri(); in WordPress? Is it not working as expected?
A shortcut intstead of always writing your full theme path is to use a quick function to do this for you. Also, the theme path will then work even if you move your site. If get_template_directory or get_template_directory_uri is not working in WordPress, Try these solutions below:

 

get_template_directory not working in Worpress

The get_template_directory(); retrieves the absolute path to the directory of the current theme (parent theme) without the trailing slash and including the www- or public_html – folder in your webhotel. If you are using a childtheme, use get_template_directory_uri(); or get_stylesheet_directory_uri(); instead.

 

get_template_directory_uri not working in Worpress

The get_template_directory_uri(); is returning the parent theme path and should be used if you have a childtheme. However, the get_template_directory_uri(); is returning the path of the parent-theme and not the actual child-theme path, for example:
http://my-site.com/wp-content/themes/twentythirteen

To write out the parent-theme path, don’t forget the “echo” in php, something like this:
<img src="<?php echo get_template_directory_uri(); ?>/images/my-image.jpg" />

 

Solution – use get_stylesheet_directory_uri

Use the Worpdress function get_stylesheet_directory_uri(); instead. This will return the right path of your child-theme, for example:
http://my-site.com/wp-content/themes/my-child-theme

To write out your child-theme path, don’t forget the “echo” in php, something like this:
<?php echo  get_stylesheet_directory_uri() ?>

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *