1

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:

enter image description here

2
  • use wp_enqueue_style only in the header file
    – mmm
    Apr 4, 2016 at 6:18
  • Use hook get_header please take a look @toscho answer, you have the same issue with question.
    – Jevuska
    Apr 4, 2016 at 8:28

3 Answers 3

0

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.

2
  • I think you missed, header-full.php is not page a template :D
    – Sumit
    Apr 4, 2016 at 6:58
  • thanks, I will use something llike this: if( $CurrentTemplate == 'Full Header'){ //enque CSS for header.php } Apr 4, 2016 at 10:20
0

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" />
0
-1

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>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.