is_front_page not working in functions.php

Even though this question may get down voted – because not enough information has been provided and it’s fairly easy to find tutorials elsewhere, I’ll help you out.

In your functions.php file in your theme, you need to do this:

add_action( 'wp_enqueue_scripts', 'wp5849_scripts' );
function wp5849_scripts() {
    if ( is_front_page() ) :
        wp_enqueue_style('TBDhome', get_stylesheet_directory_uri() . '/css/TBDhome.css');
    endif;
}

What this is doing:
The first line add_action is the hook to run your function wp5849_scripts() at a certain point during WordPress’s internal setup. You can read more about action hooks and filters in the WordPress codex.

The second part is the actual function to enqueue the stylesheet. Checks to see if it’s the front page of the site, if it passes, it will load the stylesheet.

I updated the directory to be get_stylesheet_directory_uri() because if you are using a child theme, get_template_directory_uri() will return the PARENT theme directory. get_stylesheet_directory_uri() will work with whatever theme is currently active (parent or child).

You can read more on the arguments that are passed to wp_enqueue_style here

As a note: you may already have a function that is enqueuing scripts and styles in your functions.php (look for the add_action( 'wp_enqueue_scripts') hook). If that’s the case, you simply need to add:

if ( is_front_page() ) :
        wp_enqueue_style('TBDhome', get_stylesheet_directory_uri() . '/css/TBDhome.css');
endif;

to the function that the add_action is calling. I wanted to add this as an option.

EDIT:
It appears there may be a larger theme issue or setting issue, based on OP comments. Here is an option to place the stylesheet in the header.php

if ( is_front_page() ) {
    echo '<link rel="stylesheet" id="TBDHome-css"  href="' . get_stylesheet_directory_uri() . '/css/TBDhome.css" type="text/css" media="all" />
}

Leave a Comment