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.

I'm trying to load scripts only when needed on a particular page.

I've got the following in my footer, but am struggling with the 'is nots' and also chaining the PHP together in an else if statement - which i think will be more elegant than the way i've written it now.

Code is as follows:

<?php if (is_page('home') ) { ?>
<script type="text/javascript" src="<?php echo bloginfo('template_directory');?>/scripts/jquery.jcarousel.min.js"></script> 
<script type="text/javascript">
jQuery(function($){
$('#mycarousel').jcarousel();
});
</script>
<?php } ?>


<!--news page lightbox custom JS--> 
<?php if (is_single() ) { ?>
<script type="text/javascript" src="<?php echo bloginfo('template_directory');?>/scripts/slimbox2.js"></script>
<?php } ?>


<!--JS to load on pages apart from model pages AND news pages--> 
<?php if ( ! is_page_template('modelPages.php') || ! is_single() ) { ?>
<script type="text/javascript" src="<?php echo bloginfo('template_directory');?>/scripts/jquery.easing.min.js"></script> 
<script type="text/javascript" src="<?php echo bloginfo('template_directory');?>/scripts/supersized.3.2.7.min.js"></script>
<?php } ?>

Can anyone a) point me in the right direction of loading scripts only if the page is NOT single or using the template and also, advise who to write this in an else statement, please?

Thanks

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You should be using wp_enqueue_script rather than putting script tags directly in template files:

function wpa61143_enqueue_scripts() {
    if( is_page( 'home' ) ) :
        wp_enqueue_script( 
            'jcarousel',
            get_template_directory_uri() . '/scripts/jquery.jcarousel.min.js',
            array( 'jquery' ),
            null,
            true,
        );
    elseif( is_single() ) :
        // enqueue scripts for single
    elseif( ! is_page_template('modelPages.php') ) :
        // enqueue scripts for other
    endif;
}    

add_action( 'wp_enqueue_scripts', 'wpa61143_enqueue_scripts' );
share|improve this answer
    
Ok - fairly new to Wordpress - is this a safer way of loading scripts then? What is the wpa number? Thanks :) –  V Neal Aug 7 '12 at 20:42
    
@VNeal - yes, that's the recommended way to add scripts. the wpa number is just a unique name based on the id of this question, you can change it to something unique that is meaningful to you. –  Milo Aug 7 '12 at 22:06
    
Could you also advise on how the correct way to add an 'or' would be, I'd like the 2nd else if to state that if it's not page template OR a single page, my || above didn't seem to work? –  V Neal Aug 8 '12 at 8:00
    
@VNeal it's not necessary, single posts were already caught by elseif( is_single() ) above. –  Milo Aug 8 '12 at 16:03
add comment

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.