0

The following function within my functions.php loads a JS script..

function my_scripts_method() {
    wp_enqueue_script(
        'myashdrop',
        get_template_directory_uri() . '/js/dropdown.jquery.min.js',
        array('jquery')
    );
}

add_action('wp_enqueue_scripts', 'my_scripts_method');

How could i modify this function so that the JS script is loaded only on specified pages (perhaps by providing their ID in the function's arguments..)

0

You can use conditional tag such as is_page(). Register your script on hook wp_enqueue_scripts and enqueue it in if statement :

   wp_register_script('myashdrop',
    get_template_directory_uri() . '/js/dropdown.jquery.min.js',
    array('jquery'));

if (is_page(PAGEID)) { wp_enqueue_script('myashdrop'); }
  • thanks but i am also using multiple pages and looking for optimum syntax.. can i separate PAGEID by commas ? – Mario Lorenzo Jun 20 '13 at 17:15
  • sure, in array: is_page(array(ID1,iD2)) – JMau Jun 20 '13 at 17:17
  • last question ! how can i enqueue multiple scripts at the same time using the is_page(PAGEID) syntax .. ? – Mario Lorenzo Jun 20 '13 at 18:03
  • You can use multiple register_script and wp_enqueue_script – JMau Jun 20 '13 at 18:05
  • ok so i can't use array in there... ? i need to repeat wp_enqueue – Mario Lorenzo Jun 20 '13 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.