Post Pic

How to: Embed CSS in your posts with a custom field

Sometimes, a specific post needs to be styled with some custom css styles. Sure, you can directly embed CSS in your post, but that not very clean. In this recipe, I’m going to show you how to embed css in a clean way, by using a custom field.

First, open your header.php file and insert the following code between the <head> and </head> html tags:

<?php if (is_single()) {
    $css = get_post_meta($post->ID, 'css', true);
    if (!empty($css)) { ?>
        <style type="text/css">
        <?php echo $css; ?>
        </style>
    <?php }
} ?>

Once done, when you're writing a post or page which require some custom css styling, you'll just have to create a custom field named css and paste your custom css styles as a value. That's simple as that!

38 Responses

Dec 29 2008 10:09

Yeah. Thanks for this great recipe ;-)
I love your blog.

Dec 29 2008 10:49

@Jared: Thanks for the compliments :)

Dec 29 2008 13:16

Hmm, I like the idea. But I would suggest to change the class of the post with the custom field and have the CSS in the style sheet. That way you keep the style and content apart. If I want to change theme later on, all custom CSS won’t haunt me then. :D

Dec 29 2008 18:02

Nice recipe :D

Im a big fan :D

Dec 29 2008 21:50

@Yannick: Sure it is possible (and a good idea) to do that. But sometimes, even if embedding css directly in html files isn’t pretty, it can be useful :)

@Dr.WordPress: Thanks for your support! You WpRecipes readers are AWESOME!

Dec 30 2008 13:25

Nice idea.
Wrap a function around and put it in your functions.php like:

function my_custom_css() {
.. your code
}
add_action('wp_head', 'my_custom_css');

Dec 31 2008 07:34

Thanks for the tip! I can’t wait to try this out on my blog! :D

Jan 09 2009 14:24

How about using another custom field for different post? can I use this code too? what’s the code exactly? thanks for the recipes :)

Feb 04 2009 13:45

I have tried it and have two comments:
a) As it is written it only appears when you are looking at the post itself. Else you must eliminate in the first line “if (is_single()) {” and in the last “}”
b) it must be put before the call wp_head (at least in the theme I’m using

Apr 16 2009 19:12

This is freaking crazy great.

Jul 08 2009 04:57

I think you forgot to close your tag

Also: what’s the best way of using this technique for the main loop? As in, not just for single pages

Jul 08 2009 04:58

Your form ate part of my post – I was referring to the style tag (it opens twice)

Aug 19 2009 19:35

Hello, can u help me out on sumthing?!

Ill try to explain:
My website will have usual posts and book posts. In the book posts, i want the page to show the publisher, the author etc, in custom fields. But I dont want it to be shown in normal posts.
Is there a code (visible/hide tables) that I can use, IF the post contains the custom field type book?

Sep 14 2009 20:47

Could you please give a brief example of this? I don’t understand what “a custom field named css” and “paste your custom css styles as a value” means. Thanks.

Sep 15 2009 04:25

Is this code complete? I’ve gotten some errors and it looks like the tag wasn’t close completely. Thanks for sharing though.

Sep 29 2009 06:52

I don’t know… it’s just not making sense to me… If I label a custom field with a name of “css” and a value of “{font-size:20px;}”, what do I have to do in the content to apply it? I’ve tried wrapping it in span, etc.

Can you show a screen-cap or something for how to use this? Thanks!

Sep 29 2009 07:10

@len: easy: Just put #content{font-size:20px;} in your custom field.

Nov 16 2009 01:18

Jean Baptiste, this was a handy tip.

As Bretton pointed out though, it looks like you forgot to put in a “/” to close the style block correctly…

I also had to do what fahirsch suggested for it to work in my theme.

Otherwise works great.

Nov 16 2009 09:02

@Hank : Sorry for that mistake! It is corrected now :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required