On one page of my WordPress site i use a photo-gallery. For this i need a js-file. Now the js-file is loading on everypage but its just needed one this specific page with the page id 2. Now the problem is, that is_page(2) is not working in the functions.php because the page id is not set then. Is it still possible to do this via functions.php or would you just add the script in the footer.php?
function footer_scripts()
{
// If query if current Page is not wp-login-php or admin page
if ($GLOBALS['pagenow'] != 'wp-login.php' && !is_admin()) {
wp_register_script('photo-gallery', get_template_directory_uri() . '/js/back-layer-photo-gallery.js', array('jquery'), '1.3', true);
wp_enqueue_script('photo-gallery');
}
}
add_action('init', 'footer_scripts');
2
Answers
you can add this code in function.php and fill it with the slug name of your page and of your script
You can use page slug instead of page ID and use ‘wp_enqueue_scripts’ instead of ‘init’. If you are using child theme use ‘get_stylesheet_directory_uri()’ in place of ‘get_template_directory_uri()’ otherwise you can leave it as it is.