Stylesheet not linking

by Swe   Last Updated May 28, 2020 05:08 AM

So I have the following structure in my theme:

Themes/
- theme-name/
-- functions.php
-- style.css
-- page-directories.php

For some reason, I'm unable to link the style.css to be included for my page-directories.php file.

Here is what I have inside the functions.php:

function register_directories_style() {
    wp_register_style('style', get_template_directory_uri(), [], 1);
    wp_enqueue_style('style');
}
add_action( 'wp_enqueue_scripts', 'register_directories_style');

Here is what's inside my page-directories.php file:

<?php

/* Template Name: Directories */

?>

<!-- All CDN Scripts to be converted later -->
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" rel='stylesheet' type='text/css'>

<!-- The actual panel -->
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default panel-table">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col col-xs-6">
                            <h3 class="panel-title">Panel Heading</h3>
                        </div>
                        <div class="col col-xs-6 text-right">
                            <button type="button" class="btn btn-sm btn-primary btn-create">Create New</button>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <table class="table table-striped table-bordered table-list">
                        <thead>
                        <tr>
                            <th><em class="fa fa-cog"></em></th>
                            <th class="hidden-xs">ID</th>
                            <th>Name</th>
                            <th>Email</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td align="center">
                                <a class="btn btn-default"><em class="fa fa-pencil"></em></a>
                                <a class="btn btn-danger"><em class="fa fa-trash"></em></a>
                            </td>
                            <td class="hidden-xs">1</td>
                            <td>John Doe</td>
                            <td>johndoe@example.com</td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

I have the template selected on the page, but for some reason the style.css stylesheet won't apply to the page.

Also, how would I be able to select a stylesheet inside a folder such as css/directories.css?



Answers 1


get_template_directory_uri() just returns the directory of the current theme, not the link to the actual stylesheet.

In functions.php try:

function register_directories_style() {
    wp_register_style('style', get_template_directory_uri().'/style.css', [], 1);
    wp_enqueue_style('style');
}
add_action( 'wp_enqueue_scripts', 'register_directories_style');

if you wanted to select a different stylesheet, you just updated the $src arguement. e.g.

function register_directories_style() {
    wp_register_style('directory_style', get_template_directory_uri().'/css/directories.css', [], 1);
    wp_enqueue_style('directory_style');
}
add_action( 'wp_enqueue_scripts', 'register_directories_style');
mtedwards
mtedwards
May 28, 2020 04:51 AM

Related Questions


How to add dynamic inline style?

Updated September 09, 2017 21:08 PM

Advantages of using <link> instead of wp_enqueue_style()?

Updated September 27, 2017 03:08 AM

Font Awesome 5 Free - far working but fas is not?

Updated July 24, 2018 02:08 AM

How do I organize custom color support in a WP theme

Updated September 10, 2017 14:08 PM

I can't add CSS with functions.php

Updated February 26, 2018 17:08 PM