- This topic has 7 replies, 3 voices, and was last updated 1 year, 6 months ago by
Elvin.
-
AuthorPosts
-
January 12, 2021 at 6:09 am #1615613
Davide
Hello there! I am trying to enqueue my scripts in child
functions.php
withfunction custom_js() { wp_enqueue_script( 'theme_js', get_stylesheet_directory_uri() . '/js/davide_scripts.js', array( 'jquery' ), '1.0', true ); echo(get_stylesheet_directory_uri() . '/js/davide_scripts.js'); // Debugging, check path to be correct } add_action( 'wp_enqueue_scripts', 'custom_js' );
but it does not work. The same .js file hooked to
wp_head
with<script type="text/javascript" src='https://softgunners.net/wp-content/themes/generatepress_child/js/davide_scripts.js'> </script>
works fine.
Any suggestion?
January 12, 2021 at 8:51 am #1616018David
StaffCustomer SupportHi there,
your wp_enqueue_script arguments are incorrect, see here for an example of the correct method:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/#comment-274
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/January 12, 2021 at 9:55 am #1616081Davide
Hello David,
why are my arguments incorrect? They are the same as in the example you provided, but for the path:
get_stylesheet_directory_uri()
poins to the child theme root folder, where my file is located, whileget_template_directory_uri()
as in the example points to the parent theme root folder.January 13, 2021 at 8:17 am #1617330David
StaffCustomer SupportTry this method:
add_action( 'wp_enqueue_scripts', 'add_my_script' ); function add_my_script() { wp_register_script( 'theme_js', get_stylesheet_directory_uri() . '/js/davide_scripts.js', array('jquery') ); wp_enqueue_script('my-accordion-script'); }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/January 14, 2021 at 1:53 pm #1619102Davide
Thanks, this works!
Since now both options are available (wp_enqueue_scripts and src hook in wp_head), which one is better? Consider I only need the script for one post category, and the hook allows me to load it only for that category.
January 14, 2021 at 6:23 pm #1619306Elvin
StaffCustomer SupportSince now both options are available (wp_enqueue_scripts and src hook in wp_head), which one is better? Consider I only need the script for one post category, and the hook allows me to load it only for that category.
You can set a condition for
add_action( 'wp_enqueue_scripts', 'add_my_script' );
so it only loads on certain category.Example: (For single posts under category “test_category”)
if(is_singular() && in_category('test_category')){ add_action( 'wp_enqueue_scripts', 'add_my_script' ); }
A wise man once said:
"Have you cleared your cache?"January 15, 2021 at 7:31 am #1620144Davide
Lovely, thanks a lot! ๐
Edit: for future reference, the provided code did not actually work: I had to put the if statement inside the function to make it work (https://wordpress.stackexchange.com/questions/310865/is-singular-and-is-home-not-working)
add_action( 'wp_enqueue_scripts', 'add_my_script' ); function add_my_script() { if(is_singular() && in_category('Recensioni')) { wp_register_script( 'review_grid_js', get_stylesheet_directory_uri() . '/js/review-grid.js', array('jquery') ); wp_enqueue_script('review_grid_js'); } }
January 17, 2021 at 5:21 pm #1622848Elvin
StaffCustomer SupportNice one. Glad you got it sorted. ๐
A wise man once said:
"Have you cleared your cache?" -
AuthorPosts
- You must be logged in to reply to this topic.