Last active
September 23, 2020 02:06
-
-
Save mikeott/1b167d10ec99ac7cde855b0a7717d54f to your computer and use it in GitHub Desktop.
Load Custom Page CSS (WordPress)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /* | |
Check if css file of slug name exists and if it does, include stylesheet for that slug. | |
Place in functions.php or make it a plugin. | |
What is this for? | |
If you need to add some custom CSS for a particular page, then this script will make that possible. | |
Simply create a .css file with the name of the page slug (example: about-our-company.css) and place it in the theme /css/ folder. | |
This CSS file will be automatically loaded when the page is visited. | |
Note 1: The CSS file will output in the head and ideally should load after style.css. | |
Try changing the priotity (10) to a higher number until it loads after style.css. | |
Note 2: If you want the CSS file to load in the footer instead, then change 'wp_head' to 'wp_footer'. | |
*/ | |
add_action('wp_head', 'include_custom_css', 10); | |
function include_custom_css() { | |
global $post; | |
$post_slug = $post->post_name; | |
$filename = get_template_directory() . '/css/' . $post_slug . '.css'; | |
if (file_exists($filename)) { | |
echo '<link rel="stylesheet" id="' . $post_slug . '-css" href="' . get_template_directory_uri() . '/css/' . $post_slug . '.css" type="text/css" media="all" />'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment