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 trialKristin Koch
394 PointsCSS and Javascript won't load onto site
So I've created my header.php and added this function
<?php wp_head(); ?>
but the css and javascript won't load to the site. I've gone over my functions.php code several times and can't find any errors. Any help is appreciated.
<?php
function wpwm_theme_styles() {
wp_enqueue_style( 'googlefont_css', 'https://fonts.googleapis.com/css?family=Permanent+Marker' );
wp_enqueue_style( 'main_css', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'wpwm_theme_styles' );
function wpwm_theme_js() {
wp_enqueue_script( 'main.js', get_template_directory_uri() . '/main.js', array('jquery'), ' ' , false);
}
add_action( 'wp_enqueue_scripts', 'wpwm_theme_js' );
?>
2 Answers
Jonathan Grieve
Treehouse Moderator 91,253 PointsHave look at your actions codes.
add_action( 'wp_enqueue_scripts', 'wpwm_theme_styles' );
add_action( 'wp_enqueue_scripts', 'wpwm_theme_js' );
In the first one it loojks liek you're trying to enqueue a script file when you want a style sheet. Looking at the documentation the scripts hook should work but you're only trying to enqueue styles on the first one.
So my suggestions are to change 'wpwm_theme_styles to wpwm_theme_scripts
Or changing your hooks to
add_action( 'wp_enqueue_script', 'wpwm_theme_styles' );
add_action( 'wp_enqueue_script', 'wpwm_theme_js' );
Kristin Koch
394 PointsDot! my fault. I named it function.php and not functions.php. That one s made the whole difference in the world
Kristin Koch
394 PointsKristin Koch
394 PointsI tried both suggestions and it didn't work.
So should you still use
'wp_enqueue_style'
when you are loading up a css page in the action codes
add_action( 'wp_enqueue_style', 'wpwm_theme_styles' );
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsYes I would do that.
I've been trying to find the code I used to do this for the Bootstrap to JS course but I'm off my computer at the moment.
But I'm positive it should work with wp_enqueue_style and wp_enqeue_script should both work, for stylesheets and scripts.