Load css / javascript on specific page wordpress

Posted: February 13, 2014 in web

Under your themes functions.php

...
function rga_scripts() {
    wp_enqueue_style('rga-style', get_stylesheet_uri());
    wp_enqueue_script('rga-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true);

    // both front and search page
    if (is_front_page() || isSearchPage()) {
        wp_enqueue_style('cycle', get_template_directory_uri() . '/css/libs/hover_image_cycle/cycle.css');
        wp_enqueue_style('jquery.nouislider', get_template_directory_uri() . '/css/libs/range_slider_2/jquery.nouislider.min.css');
        wp_enqueue_style('custom-slider', get_template_directory_uri() . '/css/libs/range_slider_2/custom-slider.css');
        wp_enqueue_script('cycle', get_template_directory_uri() . '/js/libs/hover_image_cycle/jquery.cycle.all.js', array('jquery'));
        wp_enqueue_script('nouislider', get_template_directory_uri() . '/js/libs/range_slider_2/jquery.nouislider.min.js');
    }

    //details page
    if (isDetailsPage() || isComparePage()) {
        wp_enqueue_style('simple-slider', get_template_directory_uri() . '/css/libs/details_image_slider/simple-slider.css');
        wp_enqueue_style('simple-slider-volume', get_template_directory_uri() . '/css/libs/details_image_slider/simple-slider-volume.css');
        wp_enqueue_style('flexslider', get_template_directory_uri() . '/css/libs/details_image_slider/flexslider.css');
        wp_enqueue_script('rga-slide', get_template_directory_uri() . '/js/libs/details_image_slider/simple-slider.min.js', array('jquery'));
        wp_enqueue_script('jquery.flexslider-min', get_template_directory_uri() . '/js/libs/details_image_slider/jquery.flexslider-min.js', array('jquery'));
        wp_enqueue_script('details', get_template_directory_uri() . '/js/details.js');
        wp_enqueue_style('details-css', get_template_directory_uri() . '/css/details.css');
        wp_enqueue_style('jquery-ui-css', 'http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css');
        wp_enqueue_script('jquery-ui', 'http://code.jquery.com/ui/1.10.3/jquery-ui.js');
    }
    //search page
    if (isSearchPage()) {
        wp_enqueue_script('search-page', get_template_directory_uri() . '/js/apartments.js');
        wp_enqueue_script('complex-list', get_template_directory_uri() . '/js/complexList.js');
        wp_enqueue_style('apartments-css', get_template_directory_uri() . '/css/apartments.css');
    }
    //blog list or single blog
    if (is_archive() || is_single()) {
        wp_enqueue_style('blog-css', get_template_directory_uri() . '/css/blog.css');
    }
    //for ie8 js
    if (preg_match('/(?i)msie [1-8]/', $_SERVER['HTTP_USER_AGENT'])) {
        wp_enqueue_script('ie8js', get_template_directory_uri() . '/js/ie8.js', array('jquery'));
    }
}

add_action('wp_enqueue_scripts', 'rga_scripts');

...
function isSearchPage() {
    global $rgaTemplate;
    return $rgaTemplate == 'apartments';
}
function isComparePage() {
    global $rgaTemplate;
    return $rgaTemplate == 'compare';
}
function isDetailsPage() {
    $rDetailsPage = '#complex/#';
    return preg_match($rDetailsPage, $_SERVER['REQUEST_URI']);
}

...

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s