0

I'm registering some js scripts in my function.php, I have 2 versions of one js file, one FOR the homepage, and one for every other page.
I've managed to get this to work to register the homepage script successfully:

function home_script_method() {
    wp_register_script( 'homescript', '/wp-content/themes/template/js/menu-home-open.js' );
    if( !is_page( 'home' ) ) // If it's not the given page, stop here
    return;

    wp_enqueue_script( 'homescript' );
}

add_action( 'wp_enqueue_scripts', 'home_script_method' );

But is it at all possible to register an alternative script for every other page, i.e. if on the homepage then register menu-home-open.js, if on any other page then register menu-open.js. Is that at all possible? Can't seem to get it to work, any suggestions would be greatly appreciated!

  • Please read this answer and this answer, then edit your code :) – kaiser Jul 12 '13 at 11:48
  • @kaiser quite right, edited, cheers for that. That better? :) – user1374796 Jul 12 '13 at 12:04
  • Take a look at your callback. Afaik there should be an argument/variable that you can dump: home_script_method( $arg ) { var_dump( $arg );, but I'm not sure right now. – kaiser Jul 12 '13 at 13:29
0
add_action( 'wp_enqueue_scripts', 'ron_scripts' );
function ron_scripts(){
   if(is_home()){
     wp_register_script( 'homescript', '/wp-content/themes/template/js/menu-home-open.js' );
     wp_enqueue_script( 'homescript' );
     } else {
     wp_register_script( 'nothomescript', '/wp-content/themes/template/js/FILENAMEHERE.js' );
     wp_enqueue_script( 'nothomescript' );
    }
}

alternatively replace is_home(); with any other method or code that determines what page you are on. ex:

might work

if(site_url() == get_permalink()){
  //do home stuff
} else {
  //do not home stuff
}
| improve this answer | |

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.