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 want to include a slider in the default Wordpress theme on the home page. I did manage to get the page to show using <?php if ( is_home() ) { include ('slider.php'); } ?> but it doesn't look like it's loading the jquery script it needs or the style sheet. I know there's a conflict with scripts and Wordpress, I just don't know how to get it to work.

Sample page is at http://axiomwest.com/

How do you feed in a page with it's own style sheet and scripts?

share|improve this question
add comment

2 Answers

You need to have the script in a separate file (normally it would be filename.js; I suppose filename.php would work?).

Then, you need to register and enqueue that script file, using wp_register_script() and wp_enqueue_script()

e.g.:

function mytheme_register_custom_scripts() {
    if ( ! is_admin() ) {
        $scriptsrc = get_stylesheet_directory_uri() . '/scripts/filename.js';
        wp_register_script( 'mytheme_slider', $scriptsrc );
    }
}
add_action( 'after_setup_theme', 'mytheme_register_custom_scripts' );

function mytheme_enqueue_custom_scripts() {
    if ( is_home() ) {
        wp_enqueue_script( 'mytheme_slider' );
    }
}
add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_custom_scripts' );

Note that registering the script should happen at after_setup_theme, but is_home() will not be available at this point I don't think, which is why you need to separate the enqueueing function so that it hooks into wp_head, by which time is_home() is available.

share|improve this answer
    
wp_enqueue_scripts is more appropriate hook than wp_head for such, made for that and less likely to get timing wrong. –  Rarst Apr 11 '11 at 19:02
    
For which? The scripts could be registered at wp_enqueue_scripts, but if he only wants to enqueue the script when is_home() is true, he'll have to do that at wp_head, right? –  Chip Bennett Apr 11 '11 at 19:06
    
@Chip Bennett for either. wp_enqueue_scripts is kinda... sub-hook (did I just invent the term?..) of wp_head. –  Rarst Apr 11 '11 at 19:56
    
Rarst is right. wp_enqueue_scripts runs as part of a function hooked onto wp_head at priority 1, so wp_head at any priority greater than 0 is too late. –  John P Bloch Apr 11 '11 at 19:57
    
Okay, updated to change the enqueue to hook into wp_enqueue_scripts rather than wp_head. :) –  Chip Bennett Apr 11 '11 at 20:29
show 2 more comments
up vote 0 down vote accepted

UPDATE! What I did was just include the slider I was trying to use. I linked the jquery scripts that page needs via the <?php bloginfo('template_directory'); ?> method. I did notice that it takes a couple of seconds to load that portion of the page, but it works. I still think this can be improved, but with time constraints it will have to do. Thanks all for your comments and eagerness to help!

share|improve this answer
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.