Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Theme Development WordPress Header and Footer Templates Porting existing headers and footers into WordPress

samtruss1986
samtruss1986
11,772 Points

get_template_directory_uri 404 error

I'm following the course on here and am wondering if there would be any prefix to working locally on MAMP with a sub-folder.

I have the wpt files within a sub-folder in my htdocs and when I try to load the index.php the css & js does not load however the html does load.

When inspecting the DOM I get the following error.

http://localhost:8888/treehouseget_template_directory_uri/style.css?ver=4.7.3 Failed to load resource: the server responded with a status of 404 (Not Found)

Now, I see the error. There is no forward slash between treehouse and get_template_directory_uri

My question, is the resource path correct (I assume not) and is there anything extra I would have to put in the functions.php file to correct this.

I have done some digging on the usual internet pages but have found nothing on this.

Thanks for your reply. Regards, Sam.

1 Answer

Hey there, You're really close but there's one thing that's not quite right here: get_template_directory_uri is actually a function in WordPress, so you have to add a set of parentheses after it. If you enqueue your stylesheet (in functions.php) in the following way:

wp_enqueue_style(
  'style_css',
  get_template_directory_uri() . '/style.css'
);

(or, if you are just hard-coding the stylesheet link into your site's <head>, write it like this:)

<link rel="stylesheet" href="<?php echo get_template_directory_uri() . '/style.css'; ?>">

...then WordPress will run the get_template_directory_uri function and return the entire file path to your themes subfolder, which should make the entire path to the style.css file work.

Does that solve it for you? Let me know if you have any questions.

Since you're only trying to get your default WordPress stylesheet, you can actually just call:

<?php get_stylesheet_uri(); ?>

instead of get_template_directory_uri() . '/style.css' - because it will return the entire path including the file name to your style.css file. This only works for style.css though. Cheers!

samtruss1986
samtruss1986
11,772 Points

Excellent, Thank-you for your answer! Indeed I had missed the parentheses for the CSS but not the JS.