140 posts
    Has been part of the Envato Community for over 3 years Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 10+ items on Envato Market
+2 more
weblusive says
As the subject implies, I’m trying to conditionally load styles with no luck yet. Here is the scenario: I’m offering 5 types of sliders in a Wordpress theme. However, as only one slider can be used at a time, it’s very ineffective to load all scripts at once. Including scripts conditionally is easy. Only the chosen type of slider needs to be checked. Here is a sample code:

$slider = isset($_GET['slider_type']) ? $_GET['slider_type'] : 'default';

        if($slider == 'default')
        {
            wp_enqueue_script('roundabout-slider', get_bloginfo('template_url').'/sliders/default/jquery.roundabout.js');    
            wp_enqueue_script('roundabout-shapes', get_bloginfo('template_url').'/sliders/default/jquery.roundabout-shapes.js');    
        }
        elseif($slider == 'nivo')
        {
            wp_enqueue_script('nivo-slider', get_bloginfo('template_url').'/sliders/nivo/nivo-slider.js');    
        }

However this is not true in case of enquing the styles. They just don’t get included. Any ideas on how to solve this issue would be greatly appreciated.

3007 posts
    Has been part of the Envato Community for over 4 years Has referred 200+ members Has sold $250,000+ on Envato Market Had an item featured in an Envato Bundle
+7 more
duotive says
Had the same issue. Here is what i did. I created a function.
        function get_slider_scripts($type)
        {
            switch($type)
            {
                case 'content-slider':
                    wp_enqueue_style('duotive-content-slider-style');
                    wp_register_script('duotive-slider-calls', get_bloginfo('template_directory')."/js/get-slideshow-js.php?type=content-slider", false, null);        
                    wp_enqueue_script('duotive-slider-calls');                
                break;
            }

        }    
and called if before the wp_head() so the files get enqueued before the call for wp_head.
140 posts
    Has been part of the Envato Community for over 3 years Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 10+ items on Envato Market
+2 more
weblusive says

Thank you so much, Duotive!!! You’re a lifesaver. I’ll give it a try.

438 posts
    Has been part of the Envato Community for over 3 years Sells items exclusively on Envato Market Located in United States
christopherjon says

I do it a bit differently.

I’ll just throw the css for the sliders into my primary css and I assign different classes to each slider, .nivo, .cycle etc… cuts down unnecessary files and code.

814 posts
    Has referred 1+ members Has sold $10,000+ on Envato Market Has collected 1+ items on Envato Market Contributed a free file of the month
+3 more
rvision_ says
christopherjon said
I do it a bit differently. I’ll just throw the css for the sliders into my primary css and I assign different classes to each slider, .nivo, .cycle etc… cuts down unnecessary files and code.

+1

140 posts
    Has been part of the Envato Community for over 3 years Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 10+ items on Envato Market
+2 more
weblusive says
christopherjon said
I do it a bit differently. I’ll just throw the css for the sliders into my primary css and I assign different classes to each slider, .nivo, .cycle etc… cuts down unnecessary files and code.

That’s what I do now for sliders but imagine the scenario where you have some other specific css files applicable to only specific page (e.g., prettyPhoto for portfolio page). If I include everything in the main css file it will eventually become >1 MB which is unacceptable from the point of view of performance.

236 posts
    Has sold $10,000+ on Envato Market Has been a beta tester for an Envato feature Has collected 1+ items on Envato Market Sells items exclusively on Envato Market
+1 more
GradaStudio says
weblusive said
christopherjon said
I do it a bit differently. I’ll just throw the css for the sliders into my primary css and I assign different classes to each slider, .nivo, .cycle etc… cuts down unnecessary files and code.
That’s what I do now for sliders but imagine the scenario where you have some other specific css files applicable to only specific page (e.g., prettyPhoto for portfolio page). If I include everything in the main css file it will eventually become >1 MB which is unacceptable from the point of view of performance.

Css > 1 mb :O never seen

438 posts
    Has been part of the Envato Community for over 3 years Sells items exclusively on Envato Market Located in United States
christopherjon says
That’s what I do now for sliders but imagine the scenario where you have some other specific css files applicable to only specific page (e.g., prettyPhoto for portfolio page). If I include everything in the main css file it will eventually become >1 MB which is unacceptable from the point of view of performance.

Loading multiple files can slow down performance as well. Loading a single 30k css file is going to be faster than loading 2 10k css files.

For myself, I like to compact everything down to as few files as possible. My typical load consists of a css file, a php css file for dynamic stuff, jquery and my custon jquery file.

I’m not going to say one way is better than the other, it really depends on how you code and how your theme works.

by
by
by
by
by
by