Support » Themes and Templates » Why are all my stylesheets being called on WordPress template?

  • I’m designing a custom WP template. However, while editing in Google Dev Tools, it looked like each of my individual WP pages were calling every single stylesheet I am using. What am I doing wrong? Or am I assuming incorrectly? Here’s some code:

    Header:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <!-- Required meta tags -->
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
            <meta http-equiv="x-ua-compatible" content="ie=edge">
            <title><?php echo get_bloginfo( 'name' );?></title>
            <meta name="description" content="<?php get_bloginfo('description');?>">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
            <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_directory'); ?>/CSS/universal.css" media="screen" />
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    
    <!----------- HEADER NAVIGATION -------------------------------->
            <div id="navBackground" class="navbar-fixed-top">
                <nav class="col-xs-10 col-xs-offset-1 m-y-1">
                    <a href="/index.php">
                        <img src="<?php bloginfo('template_directory');?>/img/fullLogo.png" class="fluid-img pull-md-left" id="navLogo">
                    </a>
            </div>
    
            <?php wp_head() ?>
        </head>

    Example Custom Page:

    <?php /* Template Name: Approach Template */ ?>
        <?php get_header()  ?>
     <link rel="stylesheet" href="<?php bloginfo('template_directory');?>/CSS/approach.css">
    <!--------------- Approach Intro -------------------->
        <div id="introParagraph" class=" col-xs-12 p-y-2 m-b-3">
            <?php
                if (have_rows('approach_introduction')):while(have_rows('approach_introduction')):the_row();
            ?>
            <h1 class="text-xs-left col-xs-10 col-md-offset-1 m-b-1">
                <?php
                    the_sub_field('page_header');
                ?>
            </h1>
            <div class="col-md-3 col-md-offset-1 col-xs-7 m-t-1">
                <h3 class="text-xs-left" id="headerDescription">
                    <?php
                        the_sub_field('page_description_paragraph');
                    ?>
                    <br><br> Click on the icons to learn more.
                </h3>
            </div>
    
            <?php   endwhile;
            else :
                // no rows found
            endif;
            ?>
        </div>

    However, when I look in my dev tools, it shows this:

Viewing 3 replies - 1 through 3 (of 3 total)
  • Can anyone offer any suggestions about this?

    Hey @brandonstiles!

    Hmm, perhaps I (and others) don’t quite understand what the issue is (your screenshot doesn’t look like it attached either). When you say that all of your CSS files are being called on each of your pages, this would only be an issue if you had a different stylesheets for each of these pages. Based on the code you included, it looks like you are trying to load a stylesheet from inside the page template with this line:

    <link rel="stylesheet" href="<?php bloginfo('template_directory');?>/CSS/approach.css">

    While it’s technically possible to load an external stylesheet inside the body, it’s not recommended and your current implementation would not be valid (you can’t use the rel tag) markup.

    You should be using WordPress hooks and your functions.php file to achieve the functionality you’re after. You can use the is_page() and is_page_template() functions to load the correct file on the correct page. Here are some links that you may find useful:

    https://developer.wordpress.org/reference/functions/wp_enqueue_style/
    https://developer.wordpress.org/reference/functions/is_page/
    https://developer.wordpress.org/reference/functions/is_page_template/

    I hope that helps!

    Thanks!

    Ryan

    Hey @ryanphillips, thank you for your detailed response! I’m a bit new to the WordPress game, so I might be confused on some things. I AM trying to call a different stylesheet on all the pages (About.css for About Page, services.css for Services page, etc.).

    I’ve wp_enqueue’d all the stylesheets, so really (what I should be doing) is using the is_page() calls on each individual page for its respective CSS stylesheet?

    BTW, here’s another shot at the image to give you an idea: http://i.imgur.com/FdP14lC.png

    Thanks for helping me out!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Why are all my stylesheets being called on WordPress template?’ is closed to new replies.