I have two different files for the header in my theme. These are header.php and header-full.php.
How can I load different CSS in different headers?
I enqueue my CSS files in function.php like this:
Enqueue style depending on a template filename:
if('header.php' == basename( get_page_template() ) { // check the template file name
// enqueue header.php style here
}
if('header-full.php' == basename( get_page_template() ) { // check the template file name
// enqueue header-full.php style here
}
wp_enqueue_style already registers a style so you don't need to register a style before enqueueing it except in very special cases. See wp_enqueue_style.
header-full.php is not page a template :D
If you want to stick with your enqueued files (probably a good idea as outlined here), then - yes - you could add some conditionals in your functions.php such as:
if(some conditional) {
// enqueue header.php style here
}
if(some other conditional) {
// enqueue header-full.php style here
}
Or, you don't have to enqueue them at all. You could just reference them in your two header files. Eg.in header.php put:
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri(); ?>/bootstrap/css/custom.css" type="text/css" media="screen" />
and in header-full.php put:
<link rel="stylesheet" href="<?php echo get_stylesheet_directory_uri(); ?>/bootstrap/css/full-header.css" type="text/css" media="screen" />
One simple way of doing it is by using a php function called "basename".
If you have 2 header headerone.php, headertwo.php you can load different resources depending upon the name of the page you are viewing.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<?php if(basename($_SERVER['PHP_SELF']) == 'headerone.php'){ ?>
<link href="/includes/headerone.css" rel="stylesheet" media="all"/>
<?php }
elseif(basename($_SERVER['PHP_SELF']) == 'headertwo.php'){
?>
<link href="/includes/headertwo.css" rel="stylesheet" media="all"/>
<?php } ?>
</head>
<body>
wp_enqueue_styleonly in the header fileget_headerplease take a look @toscho answer, you have the same issue with question.