How to Use Custom Stylesheets on a Per Post Basis in Wordpress
Something I struggled with recently was assigning individual style sheets to posts on this Wordpress blog. For example, if I was writing a CSS tutorial I didn’t want to muck up my general stylesheet with a bunch of styles - it’d be nice to write a small stylesheet and attach it only to the specific post that needs it.
Unfortunately, this isn’t a built in function of Wordpress. Fortunately, you can add this functionality with a plugin.
Headspace
I originally accomplished this with the Headspace plugin.
Headspace is an all in one SEO plugin for Wordpress. It’s major function is to allow you to add custom titles, tags, and meta descriptions to your Wordpress posts. This should help with your SEO and SERP.
An added bonus is that you can add individual CSS stylesheets and JS scripts to each page. There’s an extra field in the writing page where you can add either of these two options.
This part of Headspace worked great. However, I was having trouble getting the other SEO functions to work. I was assigning descriptions and titles, but no meta tags were being generated.
Replacing Headspace
To fix that problem, I de-activated Headspace and got the All In One SEO Pack Wordpress Plugin. It worked great, but that’s another post altogether.
I didn’t realize until after I had deactivated Headspace that I had also deactivated the custom CSS on a few posts/pages. I checked the pages and low-and-behold the custom CSS didn’t exist anymore.
I didn’t want to re-active Headspace just for this function, so I went off in search of a better solution.
Per Post CSS and Javascript
I eventually stumbled on this very simple plugin - Per Post CSS and JS.
It’s only a few short lines. It uses the custom fields function of Wordpress to add the custom link tags. You define a CSS or JS field for a post, enter the URL, and the plug-in adds a link tag to the head section of your page.
It’s quite simple, and if I didn’t find a suitable script I was going to make one myself. However, this works perfectly well and I don’t think I could really expand on it (except maybe creating the CSS and JS fields on the writing screen, instead of using the custom fields) - so I doubt I’ll be writing my own plug-in anytime soon.
When it’s possible, re-use. Then focus your energy on things you need to do yourself.
Tags: css, php, plugin, stylesheet, wordpress







Leave a Reply