Search code examples
phpcsswordpressfontawesome-4.4.0

remove font-awesome from landing page of site (Wordpress)


In my header.php file, I only want to load the font-awesome css file if the page is not my main page (it is costing 1 sec of load time and barely does anything). The following code in the <head> html part, does not work:

<?php if ( !is_home() ){ ?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<?php } ?>```

Solution

  • First, if you want to target the site Front Page, you need to use is_front_page(). The is_home() conditional returns true when the blog posts index is displayed, which may or may not be on the site Front Page.

    <?php if ( !is_front_page() ){ ?>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <?php } ?>
    

    Second, you need to hook your function into an appropriate hook, which it appears, in this case, is wp_enqueue_scripts.

    function rf_enqueue_front_page_scripts() {
        if( !is_front_page() )
        {
            wp_enqueue_style( 'font-swesome', "https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" );
        }
    }
    add_action( 'wp_enqueue_scripts', 'rf_enqueue_front_page_scripts' );