linking javascript file to wordpress child theme -
i'm trying link javascript file child theme. i've looked @ wordpress codex , numerous examples, still not working. i'm working on localhost, have read, want use get_stylesheet_directory(). i've echoed out , pointing correct path. below code placed in functions.php child theme:
add_action( 'wp_enqueue_scripts', 'theme_js' ); function theme_js() { wp_enqueue_script( 'theme_js', get_stylesheet_directory() . '/js/theme.js', array('jquery'), '', true ); }
my javascript file looks this:
/** * custom theme styles */ ( function( $ ) { $('.main-navigation li a').click(function() { var link = $(this).attr('href'); $('html, body').animate({ scrolltop: $(link).offset().top }, 1500); }); })( jquery );
the src string in enqueue needs url, not path, need use get_stylesheet_directory_uri()
instead of get_stylesheet_directory()
:
add_action( 'wp_enqueue_scripts', 'theme_js' ); function theme_js() { wp_enqueue_script( 'theme_js', get_stylesheet_directory_uri() . '/js/theme.js', array('jquery'), '', true ); }
Comments
Post a Comment