Often I find myself needing to add a lot of extra styling to one or two pages in WordPress but I don’t want to bloat my theme’s style.css with lots of extra code.
With a simple PHP switch I can add additional CSS, Javascript or other items to the page head.
First create a file in your theme called header-page-includes.php. Then add a switch statement and a case for each page that you want to add custom CSS or Javascript to:
if(is_page()) { switch (get_the_title()) { case "About": echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo( 'template_url' ).'/css/about.css" />'; break; case "FAQ": echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo( 'template_url' ).'/css/faq.css" />'; echo '<script src="'.get_bloginfo( 'template_url' ).'/js/faq.js"></script>'; break; } }
I chose to evaluate the page title, but if you have multiple page titles that are the same you may want to use the page ID instead.
Then include that file in your header.php
get_template_part('header-page-includes');
Buffy says:
Hey hey hey, take a gadner at what you’ve done