Multiple css files Child theme wordpress

I hope your greate, i have some issues with loading separate files based on url variable in wordpress. I thought it would be best to do this in the child theme functions file. code below:

$getURLVar = explode("/",$_SERVER['REQUEST_URI']);

if($getURLVar[1] == "temp"){
   $uri_based_style = 'style-temp.css'; 
} else {
   $uri_based_style = 'style.css';  
}


 add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles',999 );
 function theme_enqueue_styles() {
 wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
 wp_enqueue_style( 'child-style',
  get_stylesheet_directory_uri() . $uri_based_style,
  array( $parent_style )
 );
}

I cant get it to load the file.
Both css files are based in the child theme dir.
Does anybody have a clue ?

Read More

Best Regards

Related posts

1 comment

  1. Your issue is the variable scope. You’ve defined the variable outside of the function, so it’s not available by default inside the function. For simplicity sake, just declare it inside the function, like so:

    function theme_enqueue_styles() {
        $getURLVar = explode("/",$_SERVER['REQUEST_URI']);
        if($getURLVar[1] == "temp"){
           $uri_based_style = 'style-temp.css'; 
        } else {
           $uri_based_style = 'style.css';  
        }
    
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . $uri_based_style,
            array( $parent_style )
        );
    }
    

    Alternatively…

    If for some reason you really want / need to declare it outside the function, use global, like so:

    $getURLVar = explode("/",$_SERVER['REQUEST_URI']);
    if($getURLVar[1] == "temp"){
       $uri_based_style = 'style-temp.css'; 
    } else {
       $uri_based_style = 'style.css';  
    }
    
    function theme_enqueue_styles() {
        // global in the variable here so you can use it in the function
        global $uri_based_style;  
    
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . $uri_based_style,
            array( $parent_style )
        );
    }
    

Comments are closed.