Reputation: 25
I am stuggeling to get my wordpress enque script to work. Dont know what i'm doing wrong, tried a lot but nothing seems to work.
I have a page with tittle Ledenlijst, id = 69. I want to enque only to that page. So i tried to add to functions.php :
// Funtion to include stylesheet for page ledenlijst.
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
}
// Function to add ledenlijst
function add_ledenlijst() {
wp_enqueue_script(
'your-script',
get_template_directory_uri() . '-child/ledenlijst.js',
array('jquery')
);
}
if(is_page('ledenlijst') ){ // Only on ladenlijst page
echo "Page ledenlijst identified"; // debug
// Register Stylesheet
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
// Register ledenlijst script
add_action( 'wp_enqueue_scripts', 'add_ledenlijst' );
}
For some reason it's not identified. I also tried: if(is__page(69), A have copied the default page, created pageLedenlijst.php in my child theme folder, applied that theme to this page and tried: if(is_page_template('pageLedenlijst.php')
Nothing is working, the debug is not shown, the enque schript will not load.
Upvotes: 0
Views: 1030
Reputation: 25
Thanks Marcos, It worked indeed. I tried it again, forgot to change the get_template_directory_uri().
This code works:
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
if( is_page( 69 ) ){
wp_enqueue_script('your-script', get_template_directory_uri() . '-child/ledenlijst.js', array('jquery') );
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
No need for a custom page / template anymore. Happy wwith the solution!
Upvotes: 0
Reputation: 712
Have you tried this?
You just have one function to enqueue and then you check if the page you are in is the page you wanna enqueue the script.
I think you are using a child theme and this script ( ledenlijst.js ) is in your child theme not in the parent theme, am I right? If yes is the answer you have to change get_template_directory_uri()
for get_stylesheet_directory_uri()
because get_template_directory_uri() ( when in a context of child theme) it calls for the parent theme directory and get_stylesheet_directory_uri()
refers to the child theme directory
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('parent-style') );
if( is_page( 69 ) ){
wp_enqueue_script('your-script', get_template_directory_uri() . '-child/ledenlijst.js', array('jquery') );
}
}
Resources:
get_template_directory_uri()
get_stylesheet_directory_uri()
Sorry for bad english
Upvotes: 1