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

linking javascript file to wordpress child theme

I tried asking this question on stackoverflow, but it still is not working for me. I have a jquery script that i want to run on my wordpress child theme. I have placed the following into the functions.php file in the child theme:

add_action( 'wp_enqueue_scripts', 'enqueue_and_register' );

function enqueue_and_register() { wp_register_script( 'theme', get_stylesheet_directory() . '/js/theme.js' ); wp_enqueue_script( 'theme', get_stylesheet_directory() . '/js/theme.js', array('jquery', 'theme') ); }

It finds the theme.js when I view page source, but it does not do anthing. The jquery file worked when it was in the parent theme (I'm using avenue theme). Below is the jquery script:

/**

  • scroll script */ ( function( $ ) {

$('.main-navigation li a').click(function() { var link = $(this).attr('href'); $('html, body').animate({ scrollTop: $(link).offset().top }, 1500); }); })( jQuery );

Any help would be greatly appreciated.

1 Answer

I figured it out. Just in case anyone would like to see what I did, here is the code:

add_action( 'wp_enqueue_scripts', 'enqueue_and_register' );

function enqueue_and_register() { wp_register_script( 'theme', get_stylesheet_directory_uri() . '/js/theme.js' ); wp_enqueue_script( 'theme', get_stylesheet_directory_uri() . '/js/theme.js', array('jquery'), '', true ); }