Skip to content

Instantly share code, notes, and snippets.

@mikeott
Last active July 29, 2022 07:57
Show Gist options
  • Save mikeott/2d7a70e2df8b6be75926c80da050e39c to your computer and use it in GitHub Desktop.
Save mikeott/2d7a70e2df8b6be75926c80da050e39c to your computer and use it in GitHub Desktop.
WordPress: include custom CSS file if page slug matches CSS file name.
/* 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