I haven’t found any good answers to this with searches. Everything found is for plugin scripts.
I load javascripts and css with functions in the functions file which loads them all site wide. With the exception of jquery and modernizr, the rest only need to be loaded for the homepage. The homepage is a page with a custom template which includes an image slider.
How do I only load the scripts and css for the homepage?
Hi. I do not have any experience with your type of script that registers and deregisters things to be included. But what you would want to do is test for the home page using is_home();
Something like:
if (is_home())
{
// Do stuff like register CSS and JS files.
}
You may be able to do that in your existing code just by adding an additional if statement. You already have a if (!is_admin()) in there, just put the if (is_home()) and see how it works.
Like I said, I’ve never done it that way. An alternative way would be to edit your header.php file and do a conditional to check if it is the home page and then link in the appropriate CSS and JS files.
// We are in header.php
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<title><?php
/*
* Print the <title> tag based on what is being viewed.
* We filter the output of wp_title() a bit -- see
* twentyten_filter_wp_title() in functions.php.
*/
wp_title( '|', true, 'right' );
?></title>
<link rel="profile" href="http://gmpg.org/xfn/11" />
<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
// Add in this code:
<?php if (is_home()):?>
<link rel="stylesheet" type="text/css" media="all" href="/path_to_css_file.css" />
<script type="text/javascript" src="external_javascript.js"></script>
<?php endif;?>
Something similar to that untested code. Also see this:
Loads files in footer where I want them but still loads them site wide.
Added this to the homepage template
<?php is_home(); ?>
Still loading scripts sitewide.
Just an added note;
The home page is created by adding a new WordPress page (named Homepage) and applying a custom template (named Homepage) and setting it as the homepage in the Reading setting (Front page displays as static page (Homepage)).
The blog page is created by creating a new WordPress blank page (named Postpage) and applying the default template (index.php) and setting it as the Post page in the Reading section.
You need to provide more detail because your original post said homepage…
In the settings for reading, Front page displays as a static page. This page is named ‘Homepage’ and has a custom template applied that contains an image slider that uses jquery. I want the associated javascripts and css files for this slider to only load for this page.
I use the is_admin function to replace the onboard jquery with the one in the google library, and want it and the modernizr script to load site wide.
The page that uses the index.php file as a template is named Postpage and normally would be the home page. In the reading settings it is set as the Post page.