140 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • United States
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
  • Author had a File in an Envato Bundle
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 200 and 499 users
+2 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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • United States
weblusive says

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

438 posts
  • Exclusive Author
  • Has been a member for 3-4 years
  • 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
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Serbia
  • Has been a member for 5-6 years
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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
  • United States
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
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 3-4 years
  • Bought between 1 and 9 items
  • Envato Studio (Microlancer) Beta Tester
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
  • Exclusive Author
  • Has been a member for 3-4 years
  • 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