get_template_directory() still returning path to previous theme

In WordPress, I’ve been installing and using various themes, including 2013, Roots, and now BlankSlate. I noticed that now that I’ve activated BlankSlate, the function get_template_directory() used in front-page.php is still returning the path to the Roots theme directory. My current front-page.php looks like this.

<?php get_header(); ?>
<section id="content" role="main">
       <img class="frontpageimg" src="<?php get_template_directory(); ?>/images/slogan-2.png">


       <h4>In the Los Angeles area? </h4> <p>Ask about free demo loaners! </p>
       <h4>We also take mail orders.</h4> On all orders, there is a 30-day return policy!</p> 



       <img src = "<?php get_template_directory(); ?>/assets/img/cables-ers-absorber-700.jpg"/></a>
       <img src = "<?php get_template_directory(); ?>/assets/img/blueprint-silencers-v2-700.jpg"/></a>
       <img src = "<?php get_template_directory(); ?>/assets/img/brav-front-page-700.jpg"/></a>

        <a href="contact" class="btn btn-lg btn-orange">Contact Us</a>

</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

EDIT UPDATE:
Did some some debugging as follows:

Read More

Deactivated all plugins. Logged out, cleared cache. The problem persisted.

Switched from BlankSlate (in which the problem was evident) to 2013 theme. Similar behavior, although I now can say more specifically what that behavior is. While in the 2013 theme, apparently the PHP construct <?php echo get_template_directory(); ?>/path-in-2013-theme/file.jpg works, but <?php get_template_directory(); ?> returns the path to the Roots theme. The same is true of <?php echo get_template_directory_uri(); ?> (works correctly with the echo but not without).

However, the behavior is somewhat different in BlankSlate. Without the echo both get_template_directory() and get_template_directory_uri() return the path to Roots theme, while with the echo they do something else (but I’m not sure what that something else is.. how do I dump these values for debugging?)

FURTHER DEBUGGING:

I was able to inspect the page source with Firebug. Sure enough, the functions had no output without echo. So the image source was src="/assets/img/brav-banner-3.jpg and so forth. And that worked in some themes. I mean I could see the image. So maybe it’s a caching problem? When I added the echo and changed the path to the jpg, I was able to get it to work in the BlankSlate theme. So I am going to mark this solved, as a caching issue.

Related posts

1 comment

  1. The get_template_directory() and get_template_directory_uri() functions don’t actually echo anything at all. They will make no output at all unless you echo them.

    So your code that looks like this:

    <img class="frontpageimg" src="<?php get_template_directory(); ?>/images/slogan-2.png">
    

    Should actually cause this output:

    <img class="frontpageimg" src="/images/slogan-2.png">
    

    Which may not be what you’re expecting. Examine the source of the page. And add that echo, it’s not optional. 🙂

    Also, if the theme may have screwed with relative urls and rewrites and such, as Chip suggests, then your browser is caching. Browser caching is notoriously difficult to debug. I like to use Chrome and the inspector tool, because when you have it open, right-clicking on the reload button gives you a “Empty Cache and Hard Reload” which does more what you expect it to do as far as showing the real truth of the matter.

Comments are closed.