Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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..)

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

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'); }
share|improve this answer
    
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
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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