Last active
July 29, 2022 07:57
-
-
Save mikeott/2d7a70e2df8b6be75926c80da050e39c to your computer and use it in GitHub Desktop.
WordPress: include custom CSS file if page slug matches CSS file name.
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
/* Check if css file of slug name exists and if it does, include stylesheet for that slug */ | |
function check_for_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" />'; | |
} | |
} | |
add_action('wp_footer', 'check_for_css'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment