Embed CSS in your wordpress posts with a custom field

December 29, 2008 · Print This Article

Sometimes, a specific post needs to be styled with some custom css styles.

You can directly embed CSS in your post, but that not very clean.

In this recipe, we will 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!


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.